Is there any way to subscribe and to listen feed in boost beast websocket?

446 views Asked by At

I am using an example from boost beast documentation for async websocket client. The to send some command to subscribe and then to listen feed until it is terminated manually (preferably with other function or else)

Standard examples aren't applied as either it gives no response or it doesn't give permanent feed.

For references Poloniex Websocket API needs to be accessed.

Is there any example of listening to feed permanently?

1

There are 1 answers

0
sehe On

The idea is to not stop "listening" (and by listening you really just mean reading).

In synchronous code, you just loop the read operation.

In async code, you make it a chain. The echo examples in Asio show this (in several ways).

In this async websocket example you'd change the on_read completion handler

void
on_read(
    beast::error_code ec,
    std::size_t bytes_transferred)
{
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail(ec, "read");

    // Close the WebSocket connection
    ws_.async_close(websocket::close_code::normal,
        beast::bind_front_handler(
            &session::on_close,
            shared_from_this()));
}

To replace async_close with more async_reads e.g.

    // Read a message into our buffer
    ws_.async_read(
        buffer_,
        beast::bind_front_handler(
            &session::on_read,
            shared_from_this()));