0

I am new to boost library. I am writing a WebSocket server. But facing a problem that when I send a message to a client 2 messages concatenate like first I send "Hello" and then "World". but the client received "HelloWorld". I am attaching my code as a zip file. To handle this I have done this kind of change in code:

beast::flat_buffer m_WriteBuffer;
    boost::beast::ostream(m_WriteBuffer) << message;
    ws_.async_write(m_WriteBuffer.data(),
                beast::bind_front_handler(
                    &WebSocketSession::on_write,
                    shared_from_this()));
        Sleep(100);

I have added the sleep after the send then it works fine. I have also used option TCP NO_Delay but still, the issue is there.

Declaration:

boost::asio::io_context g_ioc{ 10 };

ssl::context g_ctx{ ssl::context::tlsv12 };

tcp::acceptor g_acceptor(net::make_strand(g_ioc));`

    g_acceptor.set_option(tcp::no_delay(true), ec);
    if (ec)
    {
    stl::log::trace(stl::log::LOG_GROUP_ERROR, stl::log::LOG_LEVEL_ERROR, "WebSocketListener::WebSocketListener set_option(No delay) failed, error message: %s", ec.message().c_str());
    return;
    }

My code on github

how can I solve this?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • You only set the no_delay option on the acceptor (where, ironically, it might not make any difference). Instead, set it on each socket after accepting (e.g. in `on_accept`) – sehe Jul 29 '23 at 13:35
  • By the way even with no delay, I'm not convinced that packets won't be combined in a hardware/software IP stack. – sehe Jul 29 '23 at 16:02
  • @sehe can you please explain how i can do that – Lalit Sharma Aug 01 '23 at 06:08
  • I just did. In all honesty I think you need to fix something else. E.g. you seem to WANT to use websocket protocol, but you're using `next_layer()` instead? Next, your rely on packets arriving according to any particular framing, which is never guaranteed for stream protocols. These problems are related because websockets DO have message framing built-in. Then there is many code smells like unneccesary use of manual new/delete. – sehe Aug 01 '23 at 13:28

0 Answers0