将 Websocket 与 Poco 库连接

Connect Websocket with Poco libraries(将 Websocket 与 Poco 库连接)

本文介绍了将 Websocket 与 Poco 库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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.

这篇关于将 Websocket 与 Poco 库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 Websocket 与 Poco 库连接

基础教程推荐