fix: unhandled connection closing in websocker_server_awaitable

This commit is contained in:
Anefu
2023-12-23 14:30:29 +01:00
committed by Mohammad Nejati
parent f05413912e
commit b1779ced98

View File

@ -63,8 +63,10 @@ do_session(stream ws)
// Accept the websocket handshake // Accept the websocket handshake
co_await ws.async_accept(); co_await ws.async_accept();
try
{
for(;;) for(;;)
try { {
// This buffer will hold the incoming message // This buffer will hold the incoming message
beast::flat_buffer buffer; beast::flat_buffer buffer;
@ -74,20 +76,12 @@ do_session(stream ws)
// Echo the message back // Echo the message back
ws.text(ws.got_text()); ws.text(ws.got_text());
co_await ws.async_write(buffer.data()); co_await ws.async_write(buffer.data());
}
} }
catch(const boost::system::system_error & se) catch(const boost::system::system_error & se)
{ {
if (se.code() == beast::websocket::error::closed || se.code() == boost::asio::error::operation_aborted) if (se.code() != websocket::error::closed)
{ throw;
std::cout << "Connection closed by client." << std::endl;
co_return;
}
else
{
std::cerr << "Error: " << se.what() << std::endl;
co_return;
}
} }
} }