MCPcopy Create free account
hub / github.com/boostorg/beast / do_session

Function do_session

example/websocket/server/coro/websocket_server_coro.cpp:45–92  ·  view source on GitHub ↗

Echoes back all received WebSocket messages

Source from the content-addressed store, hash-verified

43
44// Echoes back all received WebSocket messages
45void
46do_session(
47 websocket::stream<beast::tcp_stream>& ws,
48 net::yield_context yield)
49{
50 beast::error_code ec;
51
52 // Set suggested timeout settings for the websocket
53 ws.set_option(
54 websocket::stream_base::timeout::suggested(
55 beast::role_type::server));
56
57 // Set a decorator to change the Server of the handshake
58 ws.set_option(websocket::stream_base::decorator(
59 [](websocket::response_type& res)
60 {
61 res.set(http::field::server,
62 std::string(BOOST_BEAST_VERSION_STRING) +
63 " websocket-server-coro");
64 }));
65
66 // Accept the websocket handshake
67 ws.async_accept(yield[ec]);
68 if(ec)
69 return fail(ec, "accept");
70
71 for(;;)
72 {
73 // This buffer will hold the incoming message
74 beast::flat_buffer buffer;
75
76 // Read a message
77 ws.async_read(buffer, yield[ec]);
78
79 // This indicates that the session was closed
80 if(ec == websocket::error::closed)
81 break;
82
83 if(ec)
84 return fail(ec, "read");
85
86 // Echo the message back
87 ws.text(ws.got_text());
88 ws.async_write(buffer.data(), yield[ec]);
89 if(ec)
90 return fail(ec, "write");
91 }
92}
93
94//------------------------------------------------------------------------------
95

Callers

nothing calls this directly

Calls 6

set_optionMethod · 0.80
async_readMethod · 0.80
textMethod · 0.80
failFunction · 0.70
decoratorClass · 0.50
dataMethod · 0.45

Tested by

no test coverage detected