diff --git a/client_server/websocket_client.cc b/client_server/websocket_client.cc index 55eddb2523..4e665830bf 100644 --- a/client_server/websocket_client.cc +++ b/client_server/websocket_client.cc @@ -182,7 +182,8 @@ void websocket_client::do_write() [this](boost::beast::error_code ec, std::size_t bytes_transferred) { on_write(ec, bytes_transferred); }); - } else { + } + else { ws_stream_->async_write( msg.buffer->data(), [this](boost::beast::error_code ec, std::size_t bytes_transferred) { diff --git a/client_server/websocket_server.cc b/client_server/websocket_server.cc index ba58f2afda..1cd7bf64fb 100644 --- a/client_server/websocket_server.cc +++ b/client_server/websocket_server.cc @@ -124,8 +124,12 @@ void websocket_server::connection::send(const std::string& message) { if (!is_websocket_) return; - message_queue_.push(message); - + queued_message msg; + msg.data = message; + msg.buffer = std::make_shared(); + boost::beast::ostream(*msg.buffer) << msg.data; + + message_queue_.push(msg); if (!writing_) { do_write(); } @@ -140,10 +144,9 @@ void websocket_server::connection::do_write() writing_ = true; auto msg = message_queue_.front(); - message_queue_.pop(); ws_stream_->async_write( - boost::asio::buffer(msg), + msg.buffer->data(), [self = shared_from_this()]( boost::beast::error_code ec, std::size_t bytes_transferred) { self->on_write(ec, bytes_transferred); @@ -167,7 +170,8 @@ void websocket_server::connection::close() void websocket_server::connection::on_write(boost::beast::error_code ec, std::size_t) { - if (ec) { + message_queue_.pop(); + if(ec) { server_.remove_connection(id_); return; } diff --git a/client_server/websocket_server.hh b/client_server/websocket_server.hh index 16e7f73af1..b5b19817a5 100644 --- a/client_server/websocket_server.hh +++ b/client_server/websocket_server.hh @@ -76,7 +76,11 @@ class websocket_server { websocket_server& server_; id_type id_; - std::queue message_queue_; + struct queued_message { + std::string data; + std::shared_ptr buffer; + }; + std::queue message_queue_; bool writing_{false}; bool is_websocket_{false}; };