c++ - QWebSocket无法处理握手

标签 c++ qt websocket qtwebsockets

我在使用 QWebSocket 建立 WebSocket 连接时遇到问题。我正在使用 Qt 5.6 及其 QWebSocket 实现。我确信服务器工作正常并使用 WS v13 协议(protocol)。它适用于所有浏览器。当我尝试连接到服务器时,QWebSocket 会发出带有这些消息的 errorstateChanged 信号(我使用 errorString() 方法获取它们):

QWebSocketPrivate::processHandshake: Connection closed while reading header.
Invalid statusline in response: Upgrade: websocket
Invalid statusline in response: Connection: Upgrade
Invalid statusline in response: Sec-WebSocket-Accept: HLaITyncgax+CH7OUIXnsCfFDDY=
Invalid statusline in response: // there's a carriage return symbol

虽然它说读取 header 时连接已关闭,但连接实际上并未关闭(因为服务器在一段时间后通过 websocket 发送额外的数据包)。我已经在 websocket.org 的 echo 测试上测试了我的应用程序,它工作正常!但我在两次尝试中嗅探了数据包,没有发现任何显着差异。

我尝试连接的服务器上的数据包嗅探结果:

GET / HTTP/1.1
Host: <hostname>:6670
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: WcIsYc1AZ6CKikXFwGXgMg==
Origin: http://<hostname>
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Connection: keep-alive, Upgrade


HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: 7lfSGmSz/eW1mSzaMnuzOqg00w4=

echo.websocket.org连接时数据包嗅探的结果:

GET / HTTP/1.1
Host: echo.websocket.org:80
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: MUAZoP0Ef4KSWkmtsB5YDw==
Origin: http://<hostname>
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Connection: keep-alive, Upgrade


HTTP/1.1 101 Web Socket Protocol Handshake
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Headers: authorization
Access-Control-Allow-Headers: x-websocket-extensions
Access-Control-Allow-Headers: x-websocket-version
Access-Control-Allow-Headers: x-websocket-protocol
Access-Control-Allow-Origin: http://<hostname>
Connection: Upgrade
Date: Thu, 31 Mar 2016 12:05:49 GMT
Sec-WebSocket-Accept: c4yAe+A1VRuhF1LSC/TGgPoa8N4=
Server: Kaazing Gateway
Upgrade: websocket

<data exchange goes here normally>

我应该做什么来调查并解决问题?我无法访问服务器。

附注是的,我知道 QTBUG-40878 。它影响 Qt 5.3.1,但我使用 5.6.0,并且它显然已在我的 Qt 设置中修复。

最佳答案

这可能是QTBUG-51069 (这不是你提到的那个):

If there is a delay between status line ("HTTP/1.1 101 Upgrading Protocol") and headers, QWebSocketPrivate::processHandshake erroneously detects server disconnect. The problem is that disconnect condition is checked using QAbstractSocket::atEnd function which just checks if data available and not the actual socket state. This results in multiple error signals:

websocket error: QWebSocket::processHandshake: Connection closed while reading header.
websocket error: Invalid statusline in response: Date: Thu, 11 Feb 2016 20:07:37 GMT^M .
websocket error: Invalid statusline in response: Server: WSGIServer/0.1 Python/2.7.10^M .
websocket error: Invalid statusline in response: Upgrade: websocket^M .
websocket error: Invalid statusline in response: Connection: Upgrade^M .
websocket error: Invalid statusline in response: Sec-WebSocket-Accept: oQ+NK56UdGrsXFisB9chjE3SU+Y=^M .
websocket error: Invalid statusline in response: Sec-WebSocket-Version: 13^M .
websocket error: Invalid statusline in response: Content-Length: 0^M .
websocket error: Invalid statusline in response: ^M .

Qt 5.6.2 中已修复。

对于Qt 5.7,从 commit history 来看我认为将会在 5.7.1 中出现。

关于c++ - QWebSocket无法处理握手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36333135/

相关文章:

c++ - 多人游戏服务器模型——世界复制和对象更新

c++ - 以溢出安全的方式用 N 个有效的十进制数字舍入 double

c++ - 如何将 Qt 头文件包含到内核中?

python - Tornado websocket 模型

performance - 大型邮件的 Electron IPC替代品

c++ - 错误 C2679 : binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)

c++ - 是否有可能在构造函数和析构函数之外修改 `vptr`?

qt - 自定义 Qt 设计器小部件 : a scroll Area containing a custom vertical layout

android - 在不使用 qmake 的情况下在 Android 的 Qt 应用程序上使用自定义 AndroidManifest?

python - Tornado 对一个请求推送多个响应