Rasoul.A
Rasoul.A

Reputation: 15

how to write and read at the same time in boost ssl web socket server?

Currently I'm using codes of this link text and in on_read I call a function which sends some string data multiple times in a loop, using ws_write(). The problem is that I can not read from the same client that I'm writing to, in that loop.

void
    on_read(
        beast::error_code ec,
        std::size_t bytes_transferred)
    {
        boost::ignore_unused(bytes_transferred);
        do_read();
        // This indicates that the session was closed
        if(ec == websocket::error::closed)
            return;

        if(ec)
            fail(ec, "read");
        std::cout <<"!!!! WE read: "<< beast::make_printable(buffer_.data()) << std::endl;
        // Echo the message
        ws_.text(ws_.got_text());
        short h = 20;
        while(h>0)
        {

             ws_.write(boost::asio::buffer("{\"dl_rate\":1029.360857,\"ul_rate\"7426.864}"));
             h--;
             sleep(2);
        }
    }

Tried async_read but it cause code to crash.

Another problem is that, I want to catch all websocket exceptions and prevent my code from termination on exceptions like boost::wrapexcept<boost::system::system_error>, 'stream truncated' or 'Broken pipe'. Where is the proper place to insert try/catch blocks?

Please provide me an example code using the boost websocket example in the above link.

Thanks in advance.

Upvotes: 1

Views: 67

Answers (0)

Related Questions