我在使用QWebSocket
建立WebSocket连接时遇到问题。我正在使用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测试中测试了我的应用程序,它运行正常!但是我在两次尝试中都嗅到了数据包,并没有发现任何明显的差异。
我尝试连接的服务器上的数据包嗅探结果:
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>
我应该怎么做才能调查并解决问题?我无法访问服务器。
P.S。是的,我知道QTBUG-40878。它会影响Qt 5.3.1,但我使用的是5.6.0,它在我的Qt设置中已经明显修复了。
答案 0 :(得分:2)
这可能是QTBUG-51069(这不是你提到的那个):
如果状态行(“HTTP / 1.1 101升级协议”)和标头之间存在延迟,则QWebSocketPrivate :: processHandshake会错误地检测到服务器断开连接。问题是使用QAbstractSocket :: atEnd函数检查断开条件,该函数只检查数据是否可用而不是实际的套接字状态。这会导致多个错误信号:
ArrayList<AndroidApp>
修正案在Qt 5.6.2中。
对于Qt 5.7,从commit history我认为判断,它将在5.7.1中。