Eskilade
Eskilade

Reputation: 94

How to do async read/write with Beast websockets

How can I do async write and read using websockets from the Beast library? I have tried to adapted the synchronous write/read example provided in the Beast documentation here, but the code below does not behave as expected.

I expected the following output :

*launch application*
Written data ...
Received data : Hello world!
*Ctrl-C*
Closing application ...

I got this :

*launch application*
*Ctrl-C*
Closing application ...

Code :

#include <beast/core/to_string.hpp>
#include <beast/websocket.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>

/// Block until SIGINT or SIGTERM is received.
void sig_wait(beast::websocket::stream<boost::asio::ip::tcp::socket&>& ws)
{
    boost::asio::io_service ios;
    boost::asio::signal_set signals(ios, SIGINT, SIGTERM);
    signals.async_wait(
        [&](boost::system::error_code const&, int)
        {
            ws.close(beast::websocket::close_code::normal);
            std::cout << "Closing application ..." << std::endl;
        });
    ios.run();
}

int main(int argc, char *argv[])
{
    // Normal boost::asio setup
    std::string const host = "echo.websocket.org";
    boost::asio::io_service ios;
    boost::asio::ip::tcp::resolver r{ios};
    boost::asio::ip::tcp::socket sock{ios};
    boost::asio::ip::tcp::resolver::iterator iter (r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));
    boost::asio::connect(sock,iter);

    // WebSocket connect and send message
    beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock};
    ws.handshake(host, "/");
    ws.async_write(boost::asio::buffer(std::string("Hello world!")),
                   [&](beast::error_code const&)
                     {
                         std::cout << "Written data ..." << '\n';
                     }
    );

    // Register handle for async_read
    beast::streambuf sb;
    beast::websocket::opcode op;
    ws.async_read(op,sb,
                  [&](beast::error_code const&)
                    {
                        std::cout << "Received data : " << to_string(sb.data()) << '\n';
                    }
    );

    sig_wait(ws);
}

Side note: I am fairly new to the Boost library in general, so I may have gotten some of the basics wrong ...

Upvotes: 2

Views: 6563

Answers (2)

Vinnie Falco
Vinnie Falco

Reputation: 5353

There are now asynchronous WebSocket client examples that you can study or copy: http://www.boost.org/doc/libs/develop/libs/beast/doc/html/beast/examples.html

Here is an asynchronous client example which calls io_service::run() from main: http://www.boost.org/doc/libs/develop/libs/beast/example/websocket/client/async/websocket_client_async.cpp

Upvotes: 1

Micha&#235;l Roy
Micha&#235;l Roy

Reputation: 6461

You must call io_service::run(), that's the blocking call that will animate the io_service.

Upvotes: 3

Related Questions