I have a websocket server where I call websocket::stream::async_accept to perform a handshake with the client.
I also set a decorator to modify the headers like this
conn->get_websocket_stream().set_option(boost::beast::websocket::stream_base::decorator(
[this, self, conn](boost::beast::websocket::response_type& response)
{
// modify response
if (some_condition)
{
conn->close(); // is it OK?
}
});
conn->get_websocket_stream().async_accept(request, ...);
Inside the decorator, if a given condition is met, I need to abort the execution of async_accept by closing the connection to the client.
Is it allowed to close the connection to the client inside the decorator? I didn't find an answer to this question in the documentation https://www.boost.org/doc/libs/1_81_0/libs/beast/doc/html/beast/using_websocket/decorator.html
UPDATE
conn is a connection object, a normal asio session based on shared_ptr.
get_websocket_stream() returns websocket::stream
conn->close() simply closes the socket (socket used by websocket::stream)
connis or whatconn->close()ends up doing with anything we can reason about. I suspect you're going to sayget_websocket_stream()gives abeast::websocket::stream<>&andclose()callsclose()on that stream. That's would obviously NOT be valid for two reasons: 1.close()isn't valid until the ws handshake completes and 2.close()constitutes a write, and the handshake constitutes writes. You cannot overlap the writes. Instead cancel operations, the lower layer and/or raise an exceptionbeast::websocket::stream<>websocket::stream