使用Poco::Net::HTTPRequestHandler实现服务器推送事件

3
我正在尝试使用服务器发送事件将数据“流式传输”到HTML5页面。这个http://www.html5rocks.com/en/tutorials/eventsource/basics/教程对于让客户端工作非常有帮助。但是对于服务器端,我正在做类似于http://pocoproject.org/slides/200-Network.pdf中HTTPServer示例的事情。html5rocks.com教程为请求处理程序的代码提供了以下思路:
void MyRequestHandler::handleRequest (HTTPServerRequest &req, HTTPServerResponse &resp)
{
    resp.setStatus(HTTPResponse::HTTP_OK);

    resp.add("Content-Type", "text/event-stream");
    resp.add("Cache-Control", "no-cache");

    ostream& out = resp.send();

    while (out.good())
    {
        out << "data: " << "some data" << "\n\n";
        out.flush();

        Poco::Thread::sleep(500)
    }
}

和HTML5页面的源代码:

<!DOCTYPE html>
<html>
    <head>
            <title>HTLM5Application</title>
    </head>
    <body>
        <p id="demo">hello</p>
        <script>
            var msgCounter = 0;
            var source;
            var data;
            if(typeof(EventSource) !== "undefined")
            {
                source = new EventSource('/stream');
                document.getElementById("demo").innerHTML = "Event source created";
            }
            else
            {
                document.getElementById("demo").innerHTML = "Are you using IE ?";
            }

            source.addEventListener('message', function(e)
            {
                msgCounter++;
                document.getElementById("demo").innerHTML = "Message received (" + msgCounter + ") !<br/>"+ e.data;
            }, false);
        </script>
    </body>
</html>

好消息是,打开HTML页面时,数据会被流式传输,我可以得到正确的输出(在标签之间的文本按预期更新)。

问题是,当我关闭浏览器中的页面时,POCO程序会崩溃,并且我会在控制台中收到以下消息:

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3)   execution time : 22.234 s
Press any key to continue.

我正在使用Code::Blocks,因此返回值和执行时间会显示。

即使我在try{}catch(...){}之间放置while()循环,程序仍会崩溃,而没有进入catch(当我将整个main()的内容放置在try/catch之间时也是同样的情况)。

主程序仅包含这些指令:

int main(int argc, char* argv[])
{
    MyServerApp myServer;
    myServer.run(argc, argv);

    return 0;
}

我想知道是什么原因导致了这个崩溃,以及如何修复它,请帮忙解答。

非常感谢您的帮助 :)


1
嘿,你最终解决了这个问题吗?我正处于完全相同的情况中,并考虑在服务器套接字上使用反应器来手动处理关闭连接。 - Maher4Ever
当时我在Windows7操作系统上进行开发。如果我没记错的话,这就是为什么try/catch没有起作用的原因。所以最终没有在该平台上使用服务器推送事件。然而,我认为相同的代码(即带有异常处理)在Linux上应该没问题。 - maddouri
1
谢谢您的回复。我现在也正在开发一个在Windows 7上的服务器。经过研究文档,我通过注册自己的错误处理程序来处理异常,忽略了当客户端断开连接时抛出的异常。 - Maher4Ever
好的。你能分享一下你的代码吗?我认为这对于其他遇到类似问题的人会很有帮助。 - maddouri
当然可以!我回家后会发布我的代码答案。 - Maher4Ever
2个回答

2

如果有人感兴趣,我成功地解决了这个问题,方法是注册自己的错误处理程序,当SSE客户端断开连接时,简单地忽略抛出的异常:

#include <Poco\ErrorHandler.h>

// Other includes, using namespace..., etc.

class ServerErrorHandler : public ErrorHandler
{
public:
    void exception(const Exception& e)
    {
        // Ignore an exception that's thrown when an SSE connection is closed. 
        //
        // Info: When the server is handling an SSE request, it keeps a persistent connection through a forever loop.
        //       In order to handle when a client disconnects, the request handler must detect such an event. Alas, this
        //       is not possible with the current request handler in Poco (we only have 2 params: request and response).
        //       The only hack for now is to simply ignore the exception generated when the client disconnects :(
        //
        if (string(e.className()).find("ConnectionAbortedException") == string::npos)
            poco_debugger_msg(e.what());
    }
};

class ServerApp : public ServerApplication 
{
protected:
    int main(const vector<string>& args) 
    {
        // Create and register our error handler
        ServerErrorHandler error_handler;
        ErrorHandler::set(&error_handler);

        // Normal server code, for example:
        HTTPServer server(new RequestHandlerFactory, 80, new HTTPServerParams);
        server.start();

        waitForTerminationRequest();
        server.stop();

        return Application::EXIT_OK;
    }
};


POCO_SERVER_MAIN(ServerApp);

然而,我必须说这是一个丑陋的hack。此外,错误处理程序是全局的应用程序,这使它甚至不太理想作为解决方案。正确的方法是检测断开连接并处理它。为此,Poco必须将SocketStream传递给请求处理程序。


0

您可以更改代码以捕获Poco异常:

try {
    MyServerApp myServer;
    return myServer.run(argc, argv);        
}catch(const Poco::Exception& ex) {
    std::cout << ex.displayText() << std::endl;
    return Poco::Util::Application::EXIT_SOFTWARE;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接