4namespace ip = boost::asio::ip;
5namespace ws = boost::beast::websocket;
9using namespace std::chrono_literals;
18 , acceptor_(io_ctx_, { ip::make_address(
"127.0.0.1"),
port })
19 , on_message_(on_message)
25 ip::tcp::socket socket(
io_ctx_);
27 return Connection(*
this, std::make_unique<web_socket_t>(std::move(socket)));
42 ws_->text(ws_->got_text());
43 ws_->write(boost::asio::buffer(msg.data(), msg.length()));
48 boost::beast::multi_buffer buffer;
53 if (ws_->read(buffer) == 0)
55 }
catch (
const boost::system::system_error &e) {
56 const auto ec = e.code();
57 if (ec == ws::error::closed)
62 std::string msg = boost::beast::buffers_to_string(buffer.data());
63 server().on_message(msg);
69 boost::beast::multi_buffer buffer;
72 if (ws_->read(buffer) == 0)
74 }
catch (
const boost::system::system_error &e) {
75 const auto ec = e.code();
76 if (ec == ws::error::closed)
81 std::string msg = boost::beast::buffers_to_string(buffer.data());
82 server().on_message(msg);
Connection(WebSocketServer &server, std::unique_ptr< web_socket_t > ws)
void send(std::string_view msg)
boost::asio::io_context io_ctx_
boost::asio::ip::tcp::acceptor acceptor_
std::function< void(std::string)> on_message_t
WebSocketServer(uint16_t port, on_message_t onMessage)