将Websocket与Poco库连接
Connect Websocket with Poco libraries
我正在尝试使用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();
}
}
相关文章:
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 与Poco库的Websocket连接
- Poco::Net HTTPServer:如何检测断开连接的客户端?
- 使用POCO C++通过TCP连接发送和接收OOB数据
- 在 TCPServer 连接的线程上引发 POCO 事件
- 将Websocket与Poco库连接
- 如何在C++中使用Poco连接安全的websocket
- 使用POCO上传文件- SSL连接意外关闭异常
- 使用SecureStreamSocket与poco连接TCP服务器时,如何发送证书
- 我想与 POCO HTTP 服务器建立持久的 TCP 连接
- 连接尝试失败 MySQL poco.
- 使用Poco连接MySQL失败