POCO Websocket 传入消息的异步处理程序

Aysnc handler for incoming messages in POCO Websocket

我正在尝试使用 POCO 库设计 Websocket 服务器。我实现了一个简单的 class WebSocketRequestHandler: public HTTPRequestHandler 接受连接和执行任务等等 代码片段如下:

class WebSocketRequestHandler: public HTTPRequestHandler
// Handle a WebSocket connection.
{
public:
    WebSocketRequestHandler()
    {
    }

    void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
    {
        Application& app = Application::instance();
        try
        {
            std::cout << "Waiting for connection.." << std::endl;
            WebSocket ws(request, response);
            ws.setReceiveTimeout(Poco::Timespan(10, 0, 0, 0, 0)); 
            app.logger().information("WebSocket connection established!");
            int flags;
            int n;
            do
            {
                std::cout << "Waiting for incoming frame" << std::endl;
                n = ws.receiveFrame(buffer, sizeof(buffer), flags);
                std::cout << "Frame received" << std::endl;
                //Parse the frame sequentially and so on..
            }
}

我不想对服务器进行顺序操作。它应该是异步的。我尝试在 POCO 资源中查找,但找不到与之相关的任何内容。那么POCO有没有提供asyncapi呢?就像将传入的消息推送到队列中并让一个单独的线程单独处理消息,而主线程继续从客户端接收消息? 或者可能是像 boost asio api 这样的东西,通过 async_handle(socket, func*) 注册函数并在单独的线程中处理每条消息? 或者其他更好的解决方案?

SocketReactor for TCP, but there is no async HTTPServer, every HTTP requests is handled in its own thread, which is obtained from ThreadPool. While this works fine for a low request frequency, it will not scale. To scale it, you can use NotificationQueue for async notifications, or PollSet 可以对套接字 readable/writeable 状态作出反应 - 只要确保队列或轮询集是长期存在的;处理程序是短暂的并且为每个请求创建,因此您应该在 HTTP 请求工厂中创建您的处理工具。

请注意,PollSet 有一个 windows bug, which was fixed 用于下一个版本。

编辑:进一步澄清,严格来说,HTTPServer 不是顺序的,因为每个处理程序都使用一个(预创建的)线程;最好在处理函数中做尽可能少的工作,"offload" 其他地方的大部分工作量。