2

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 ...

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
Eskilade
  • 84
  • 2
  • 11

2 Answers2

3

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

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • This is not an answer. It is a question! – celavek Jun 16 '17 at 14:04
  • I was expecting the websocket stream to handle the io service behind the scene. Am I wrong in assuming so? – Eskilade Jun 16 '17 at 14:10
  • Yes, you have to call run(). That's the blocking call containing the io loop that does the magic.. – Michaël Roy Jun 16 '17 at 15:05
  • @celavek Sorry, I'll rephrase it. – Michaël Roy Jun 16 '17 at 15:08
  • I have accepted this answer as calling ios.run() just before calling sig_wait() does indeed produce the expected result. – Eskilade Jun 17 '17 at 12:47
  • Not sure how this normally works, but should @celavek remove his downvote since the answer has been refrased? – Eskilade Jun 17 '17 at 12:49
  • Thinking about it, he was right... While being helpful, my original answer was indeed in the form of a question. While that is correct in spoken english, it may not be perceived as polite by everyone. I'm glad I helped you solve your issue. That's good enough for me. – Michaël Roy Jun 17 '17 at 19:02
  • The `beast::websocket::stream` does not handle the `io_service` for you, that would be quite annoying to anyone who already has an `io_service` and associated threads. Beast specifically avoids design mistakes made by other websocket libraries. – Vinnie Falco Aug 27 '17 at 03:15
1

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

Vinnie Falco
  • 5,173
  • 28
  • 43