Simple Client

Simple client

Here is an exmaple of the client to connect the echo server:

#define _WEBSOCKETPP_CPP11_STL_
 
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/client.hpp>
 
int main() {
    std::string const url = "ws://localhost:12345/";
 
    using client = websocketpp::client<websocketpp::config::asio>;
    client c;
 
    // disable log output
    c.clear_access_channels(websocketpp::log::alevel::all);
 
    boost::asio::io_service ios;
    c.init_asio(&ios);
 
    std::size_t counter = 0;
 
    client::connection_ptr con;
 
    c.set_open_handler(
        [&con](websocketpp::connection_hdl) {
            std::cout << "connected" << std::endl;
            con->send(std::string("hello"), websocketpp::frame::opcode::text);
        });
    c.set_message_handler(
        [&con, &counter](websocketpp::connection_hdl, client::message_ptr const& msg) {
            std::cout << msg->get_payload() << " received" << std::endl;
            switch (counter++) {
            case 0:
                con->send(std::string("stop listening"), websocketpp::frame::opcode::text);
                return;
            case 1:
                con->close(websocketpp::close::status::normal, "");
            }
        });
    c.set_close_handler(
        [](websocketpp::connection_hdl) {
            std::cout << "closed" << std::endl;
        });
 
    websocketpp::lib::error_code ec;
    con = c.get_connection(url, ec);
    std::cout << ec.message() << std::endl;
 
    c.connect(con);
 
    ios.run();
}

This program connects to the echo server, and then sends "hello", after echo back message "hello" received, sends "stop listening" to the server. After receiving "stop listening" echo back message, closes the connection. After connection closed, ios.run() is automatically unblocked.

Compile

g++ -std=c++1y simple_client.cpp -o simple_client -lboost_system -pthread

Run

Run the echo_server, and then run the simple_client in a different terminal.

./echo_server
./simple_client

You get the following outputs:

Client output:

Success
connected
hello received
stop listening received
closed

Echo server output:

hello
stop listening
[2015-02-22 20:27:07] [info] asio async_shutdown error: system:9 (Bad file descriptor)
[2015-02-22 20:27:07] [info] handle_accept error: Operation canceled
[2015-02-22 20:27:07] [info] Stopping acceptance of new connections because the underlying transport is no longer listening.

Then, echo_server and simple_client finished.

Writing a sequential process

The simple client executes the sequence of the commands that are connect, send "hello", send "stop listening", and close. To write the process, I introduce counter.

    c.set_message_handler(
        [&con, &counter](websocketpp::connection_hdl, client::message_ptr const& msg) {
            std::cout << msg->get_payload() << " received" << std::endl;
            switch (counter++) {
            case 0:
                con->send(std::string("stop listening"), websocketpp::frame::opcode::text);
                return;
            case 1:
                con->close(websocketpp::close::status::normal, "");
            }
        });

However, it is not a straightforward description. Because application logics are scattered in the callback handlers. To solve this problem, I introduce Boost.Coroutine.