将 Websocket 与 Poco 库连接

2022-01-24 00:00:00 websocket connection c++ poco-libraries

我正在尝试使用 Echo Test Websockethttp://www.appinf.com/docs/poco/Poco.Net.WebSocket.html" rel="nofollow noreferrer">Poco C++ 库.为此,我的代码应该设置 Websocket:

I am trying to connect to the Echo Test Websocket using the Poco C++ libraries. In order to do so here is my code which should set up the Websocket:

HTTPClientSession cs("echo.websocket.org");
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
HTTPResponse response;

WebSocket* m_psock = new WebSocket(cs, request, response);
m_psock->close(); //close immidiately

但是它不起作用:我收到这样的错误消息:

However it does not work: I am getting an error message like this:

Poco::Exception: WebSocket Exception: Cannot upgrade to WebSocket connection: Not Found

有人可以帮忙吗?

推荐答案

未找到"错误是 HTTP 服务器返回的标准 HTTP 404 Not Found.这通常意味着您请求的资源不存在.

The 'Not Found' error is the standard HTTP 404 Not Found returned by the HTTP server. It generally means the resource you are requesting does not exist.

我通过将资源从 "/ws" 更改为 "/" 使您的代码正常工作:

I got your code to work by changing the resource from "/ws" to "/":

HTTPRequest request(HTTPRequest::HTTP_GET, "/");

并添加以下行

request.set("origin", "http://www.websocket.org");

在创建新的 WebSocket 之前.我认为这是许多(或全部?)WebSocket 服务器所期望的头对.

before creating the new WebSocket. I think it's a header pair that many (or all?) WebSocket servers expect.

相关文章