将Websocket与Poco库连接

Connect Websocket with Poco libraries

本文关键字:连接 Poco Websocket      更新时间:2023-10-16

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

有人能帮忙吗?

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

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

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

并添加以下行

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

在创建新的CCD_ 3之前。我认为这是许多(或所有?)WebSocket服务器所期望的头对。

如果您想从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();
    }
}

这是"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();
  }
}