将Websocket与Poco库连接

问题描述 投票:0回答:3

我正在尝试使用 Poco C++ 库连接到 Echo Test Websocket。 为此,我的代码应该设置 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

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

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

有人可以帮忙吗?

c++ connection websocket poco-libraries
3个回答
3
投票

“Not Found”错误是 HTTP 服务器返回的标准 HTTP 404 Not Found。这通常意味着您请求的资源不存在。

我通过将资源从

"/ws"
更改为
"/"
来让你的代码正常工作:

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

并添加以下行

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

在创建新的

WebSocket
之前。我认为这是许多(或所有?)WebSocket 服务器所期望的标头对。


3
投票

如果您想从 echo 服务器获得回复,您还必须确保使用 Http 1.1 请求。 Poco 默认为 Http 1.0。

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

这是一个完整的例子,

#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPMessage.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPClientSession.h"
#include <iostream>

using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::Net::WebSocket;


int main(int args,char **argv)
{
    HTTPClientSession cs("echo.websocket.org",80);    
    HTTPRequest request(HTTPRequest::HTTP_GET, "/?encoding=text",HTTPMessage::HTTP_1_1);
    request.set("origin", "http://www.websocket.org");
    HTTPResponse response;


    try {

        WebSocket* m_psock = new WebSocket(cs, request, response);
        char *testStr="Hello echo websocket!";
        char receiveBuff[256];

        int len=m_psock->sendFrame(testStr,strlen(testStr),WebSocket::FRAME_TEXT);
        std::cout << "Sent bytes " << len << std::endl;
        int flags=0;

        int rlen=m_psock->receiveFrame(receiveBuff,256,flags);
        std::cout << "Received bytes " << rlen << std::endl;
        std::cout << receiveBuff << std::endl;

        m_psock->close();

    } catch (std::exception &e) {
        std::cout << "Exception " << e.what();
    }

}

0
投票

这是“wss”(即安全)连接的完整示例。

#include "Poco/Exception.h"
#include "Poco/Net/AcceptCertificateHandler.h"
#include "Poco/Net/Context.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Net/Websocket.h"
#include "Poco/SharedPtr.h"
#include "Poco/URI.h"

int main(int args, char **argv) {
  Poco::URI uri("wss://ws.postman-echo.com/raw");

  Poco::Net::initializeSSL();

  Poco::Net::Context::Params params;
  params.verificationMode = Poco::Net::Context::VERIFY_NONE;
  params.verificationDepth = 9;
  params.loadDefaultCAs = true;
  params.cipherList = "ALL";

  Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::TLSV1_2_CLIENT_USE, params);

  // This accepts all certificates. Even invalid ones.
  // Use for testing only.
  Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert = new Poco::Net::AcceptCertificateHandler(false);

  Poco::Net::SSLManager::instance().initializeClient(NULL, ptrCert, context);

  auto port = uri.getPort();
  auto host = uri.getHost();
  Poco::Net::HTTPClientSession httpSession(host, port);

  auto path = uri.getPath();
  Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
  request.set("origin", "http://ws.postman-echo.com/raw");
  Poco::Net::HTTPResponse response;

  try {
    Poco::Net::WebSocket m_psock(httpSession, request, response);
    std::string testStr = "Hello echo websocket!";
    char receiveBuff[256];

    int len = m_psock.sendFrame(testStr.c_str(), testStr.length(), Poco::Net::WebSocket::FRAME_TEXT);
    std::cout << "Sent message " << testStr << std::endl;
    std::cout << "Sent bytes " << len << std::endl;
    int flags = 0;

    int rlen = m_psock.receiveFrame(receiveBuff, 256, flags);
    std::cout << "Recv bytes " << rlen << std::endl;
    std::string received(receiveBuff, rlen);
    std::cout << "Recv message " << received << std::endl;

    m_psock.close();

  } catch (std::exception &e) {
    std::cout << "Exception " << e.what();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.