2

我在使用QWebSocket. 我正在使用 Qt 5.6 及其QWebSocket实现。我确信服务器工作正常并使用 WS v13协议。它适用于所有浏览器。当我尝试连接到服务器时,QWebSocket 会发出这些消息error并发出stateChanged信号(我使用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

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

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

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>

我应该怎么做才能调查和解决问题?我无权访问服务器。

PS 是的,我知道QTBUG-40878。它会影响 Qt 5.3.1,但我使用的是 5.6.0,它显然已在我的 Qt 设置中得到修复。

4

1 回答 1

2

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

如果状态行(“HTTP/1.1 101 升级协议”)和标头之间存在延迟,QWebSocketPrivate::processHandshake 会错误地检测到服务器断开连接。问题是使用 QAbstractSocket::atEnd 函数检查断开条件,该函数仅检查数据是否可用,而不是实际的套接字状态。这会导致多个错误信号:

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,从提交历史来看,我认为它将在 5.7.1 中。

于 2016-09-05T07:32:25.667 回答