C++ websocket客户端没有接收回调

发布于 2025-01-10 15:15:24 字数 312 浏览 0 评论 0原文

我们有一个用 C# 编写的 WebSocket 服务器和客户端。服务器被设计为根据客户端读取和处理消息的快/慢来减慢速度。 C# 客户端一次读取一条消息。

我正在寻找用 C++ 编写一个客户端,到目前为止我查看的所有库都有一个用于从服务器接收消息的消息处理程序或回调机制。

这意味着客户端连续接收消息,将其排队,然后客户端从队列中读取消息。这不是我们正在寻找的行为。我们要求客户端读取一条消息并处理它,处理完成后,读取下一条消息。我们可以使用任何可用的库来实现这一目标吗?

到目前为止我已经检查了 cpprestsdk、websocketpp、libwebsocket

We've a WebSocket server and clients in C#. The server is designed to slow down depending on how fast/slow the client reads and processes the messages. The C# clients reads one message at a time.

I'm looking to write a client in c++ and all the libraries I looked so far have a message handler or callback mechanism for receiving message from server.

This would mean that the client is receiving messages continuously, queue it and the client reads from the queue. This is not the behavior we're looking for. We require the client to read a message and process it and once the processing is complete, read the next message. Is there any library available we could use to achieve this?

I've so far checked cpprestsdk, websocketpp, libwebsocket

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2025-01-17 15:15:24

您可以使用 Boost.ASIO 库。

我制作了一个可以使用 Websocket 和多个连接的服务器。每个连接都使用异步方式接收数据,这些数据将在接收下一条消息之前进行处理:

/**
* This method is used to read the incoming message on the WebSocket
* and handled it before reading the other message.
*/
void wscnx::read()
{
    if (!is_ssl && !m_ws->is_open())
        return;
    else if (is_ssl  && !m_wss->is_open())
        return;

    auto f_read = [self{shared_from_this()}](const boost::system::error_code &ec, std::size_t bytes_transferred)
                 {
                    boost::ignore_unused(bytes_transferred);

                    // This indicates that the session was closed
                    if (ec == websocket::error::closed)
                    {
                        self->close(beast::websocket::close_code::normal, ec);
                        return;
                    }

                    if (ec)
                    {
                        self->close(beast::websocket::close_code::abnormal, ec);
                        return;
                    }

                    std::string data = beast::buffers_to_string(self->m_rx_buffer.data());
                    self->m_rx_buffer.consume(bytes_transferred);

                    if (!self->m_server.expired())
                    {
                        std::string_view vdata(data.c_str());

                        /*************************************
                          Here is where the datas are handled
                        **************************************/
                        self->m_server.lock()->on_data_rx(self->m_cnx_info.id, vdata, self->cnx_info());
                    }
                    
                    /*************************************
                    Read the next message in the buffer.
                    **************************************/
                    self->read(); 
                };//lambda

    if (!is_ssl)
        m_ws->async_read(m_rx_buffer, f_read);
    else
        m_wss->async_read(m_rx_buffer, f_read);
}

在此示例中,连接可以是普通连接,也可以是安全连接,使用 SSL,两者都使用 lambda 函数来接收数据。

我在浏览器上使用 JS 应用程序测试了此服务,并且处理每条消息的顺序没有问题。

you can use Boost.ASIO Library.

I made a server that works with Websocket and multiple connections. Each connection uses an asynchronous way to receive data which will be handled before receiving the next message:

/**
* This method is used to read the incoming message on the WebSocket
* and handled it before reading the other message.
*/
void wscnx::read()
{
    if (!is_ssl && !m_ws->is_open())
        return;
    else if (is_ssl  && !m_wss->is_open())
        return;

    auto f_read = [self{shared_from_this()}](const boost::system::error_code &ec, std::size_t bytes_transferred)
                 {
                    boost::ignore_unused(bytes_transferred);

                    // This indicates that the session was closed
                    if (ec == websocket::error::closed)
                    {
                        self->close(beast::websocket::close_code::normal, ec);
                        return;
                    }

                    if (ec)
                    {
                        self->close(beast::websocket::close_code::abnormal, ec);
                        return;
                    }

                    std::string data = beast::buffers_to_string(self->m_rx_buffer.data());
                    self->m_rx_buffer.consume(bytes_transferred);

                    if (!self->m_server.expired())
                    {
                        std::string_view vdata(data.c_str());

                        /*************************************
                          Here is where the datas are handled
                        **************************************/
                        self->m_server.lock()->on_data_rx(self->m_cnx_info.id, vdata, self->cnx_info());
                    }
                    
                    /*************************************
                    Read the next message in the buffer.
                    **************************************/
                    self->read(); 
                };//lambda

    if (!is_ssl)
        m_ws->async_read(m_rx_buffer, f_read);
    else
        m_wss->async_read(m_rx_buffer, f_read);
}

In this sample the connection could be plain or secure, using SSL, both of them are using a lambda function to receive the data.

I tested this service with a JS application, on the browser and there is no problem with the sequence to attend each message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文