将WebSocket与Poco库连接

oyt4ldly  于 2023-08-05  发布在  其他
关注(0)|答案(3)|浏览(140)

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


有人能帮忙吗?

oxcyiej7

oxcyiej71#

“Not Found”错误是HTTP服务器返回的标准HTTP 404 Not Found。这通常意味着您请求的资源不存在。
我通过将资源从"/ws"更改为"/"来使您的代码工作:

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

字符串
并添加以下行

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


创建新的WebSocket。我认为这是一个标题对那么多(或所有?)WebSocket服务器期望。

velaa5lx

velaa5lx2#

如果你想从回显服务器得到回复,你还必须确保使用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();
    }

}

hgncfbus

hgncfbus3#

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

字符串

相关问题