Skip to content

Commit

Permalink
fix: unhandled connection closing in websocker_server_awaitable
Browse files Browse the repository at this point in the history
  • Loading branch information
Anefu authored and ashtum committed Dec 24, 2023
1 parent f054139 commit b1779ce
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions example/websocket/server/awaitable/websocket_server_awaitable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ do_session(stream ws)
// Accept the websocket handshake
co_await ws.async_accept();

for(;;)
try {
try
{
for(;;)
{
// This buffer will hold the incoming message
beast::flat_buffer buffer;

Expand All @@ -74,21 +76,13 @@ do_session(stream ws)
// Echo the message back
ws.text(ws.got_text());
co_await ws.async_write(buffer.data());

}
catch (const boost::system::system_error& se)
{
if (se.code() == beast::websocket::error::closed || se.code() == boost::asio::error::operation_aborted)
{
std::cout << "Connection closed by client." << std::endl;
co_return;
}
else
{
std::cerr << "Error: " << se.what() << std::endl;
co_return;
}
}
}
catch(const boost::system::system_error & se)
{
if (se.code() != websocket::error::closed)
throw;
}
}

//------------------------------------------------------------------------------
Expand Down

0 comments on commit b1779ce

Please sign in to comment.