c++websocketboost-beastbeastbeast-websockets

Call a boost function without callback


I am using boost-beast library for a websocket connection. You can refer to this example for the understanding what is happening. I have used the same example, but changed a few things:

Now I am stuck at a point where I want to do this:

void session::A(beast::error_code ec) {
    if (ec)
        return (fail(ec, "handshake"));

    ws_.async_write(net::buffer(SOMETEXT),bind(&session::B, shared_from_this(), placeholders::_1));

}
void session::B(beast::error_code ec) {
    if (ec)
        return (fail(ec, "A_FAILED"));

    if(condition1) {
        ws_.binary(true);
        ws_.async_write(net::buffer(SOMEBINARY),bind(&session::C, shared_from_this(), placeholders::_1));
    } else {
        session::on_write(ec,<WHAT SHOULD I WRITE HERE>);
    }

}
void session::C(beast::error_code ec) {
    if (ec)
        return (fail(ec, "B_FAILED"));

    ws_.binary(false);

    ws_.async_write(net::buffer(SOMETEXT),bind(&session::on_write, shared_from_this(), placeholders::_1, placeholders::_2));

}

Here is the Read function:

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

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

    // Read a message into our buffer
    ws_.async_read(
        buffer_,
        std::bind(
            &session::on_read,
            shared_from_this(),
            std::placeholders::_1,
            std::placeholders::_2));
}

QUESTION:- Please check the function B() and my question there. Any advice or answer is appreciated.


Solution

  • You can just put a 0 in the argument and mark it as unused

    e.g. session::on_write(ec, 0 /* field unused */);