For HTTP calls, you can do:
http::async_read(m_stream, m_buffer, m_res, beast::bind_front_handler(&RestSession::on_read, shared_from_this()));
// ... then
void on_read(beast::error_code ec, std::size_t /*bytes_transferred*/)
{
if (ec)
return ; // handle error_code
if (m_res.result() == http::status::not_found)
return ; // handle target not found
}
but I can't see how to do the equivalent for a websocket stream. If the stream target does not exist the on_read() is never called.
I tried getting the next stream from the websocket stream (m_websocketStream), but the lambda is never called, presumably because the websocket layer has already swallowed the HTTP response:
void on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake", m_callback);
http::async_read(m_websockStream.next_layer(), m_buffer, m_httpRes, [this, self = shared_from_this()](beast::error_code ec, std::size_t)
{
if (m_httpResponse.result() == http::status::not_found)
return fail("path not found", m_callback);
else
m_ws.async_read(m_buffer, beast::bind_front_handler(&WsSession::on_read, self->shared_from_this()));
});
}
Is this possible?