【发布时间】:2018-03-30 15:49:36
【问题描述】:
我有以下与 POCO 库相关的问题。我的客户使用 POCO 库监听来自我们后端服务器的消息。前 50 分钟一切正常,然后套接字发生了一些奇怪的事情,方法“receiveFrame”开始返回异常。之后,套接字将无法运行。我做了一些测试,之后我收到无法运行的套接字的时间正好是 50 分钟。另外我需要注意的是,我们的后端服务器始终不会发送任何内容。我不知道会发生什么...下面是我们的握手和读取程序的代码:
void WebSocketManager::Handshake()
{
qDebug() << "WebSocketManager::Handshake";
try {
HTTPResponse response;
QString origin = Settings::Instance()->GetErPortal();
QString host = origin.remove("http://");
host = host.remove('/');
QString token = "/event/bus/ws/subscribe?auth_token=" + Settings::Instance()->token().toUtf8();
_wssession.setHost(host.toUtf8().constData());
_wssession.setPort(80);
HTTPRequest request(HTTPRequest::HTTP_GET, token.toUtf8().constData(),HTTPMessage::HTTP_1_1);
request.set("origin", origin.toUtf8().constData());
_wssock = new WebSocket(_wssession, request, response);
response.getStatus();
HTTPResponse::HTTPStatus status = response.getStatus();
qDebug() << "Handshake status is : " << status;
if(status == HTTPResponse::HTTPStatus::HTTP_SWITCHING_PROTOCOLS)
_status = true;
}
catch (std::exception &e)
{
qDebug() << "WebSocketManager::Handshake exception " << e.what();
}
}
void WebSocketManager::Read()
{
char receiveBuff[1024];
while(_status)
{
qDebug() << "WebSocketManager::Read wait data...., thread = " << QThread::currentThread();
try {
int flags=0;
int rlen=_wssock->receiveFrame(receiveBuff,1024,flags);
if(!rlen)
{
qDebug() << "WebSocketManager::Read error";
emit ConnectionFailed();
return;
}
else
{
qDebug() << "WebSocketManager::Read, len =" << rlen << ", flags = " << flags << ", data = " << receiveBuff;
ProcessBackendEvent(QString(receiveBuff));
}
}
catch (std::exception &e)
{
qDebug() << "WebSocketManager::Read exception " << e.what();
}
}
}
【问题讨论】:
-
要更好地了解究竟发生了什么,请捕获 Poco::WebSocketException& 并记录
displayText()。 -
当它发生时我收到错误 11。你说的 displayText 是什么意思?
-
try { ... } catch (Poco::WebSocketException& e) { qDebug() << e.displayText(); } -
我没有看到任何 Poco::WebSocketException - 我现在使用 NetException。 displayText 产生与 what() 方法相同的输出。我使用 POCO 库版本 1.4.6
-
那是古老的。我建议的第一件事是升级到 1.9.0,看看它是否仍然发生 - 我还不清楚这是一个 poco 问题
标签: c++ qt poco-libraries