QWebSocket closeCode not returning expected value
Hello,
I have a C++ Qt application that uses QWebSocket which connects to a server that uses uWS. When the server closes the websocket and specifies a close code and close reason, QWebSocket::closeCode() returns 1002 and closeReason(): "Invalid close code 1002 detected." This is even when I have the server send a close code that is not 1002 and a customized message for close reason. It has worked correctly in some cases, but I'm still trying to find out what makes it misbehave. I first thought that it was because the server was not sending a valid close code, because initially I was using 404, which is invalid, but then I changed it to test the close code values in QWebSocketProtocol::CloseCode, but the issue persists. Is there a specific reason why QWebSocket::closeCode() would return 1002 even though the server is sending a valid close code? Any help would be appreciated.
The application uses Qt 5.12.3 with mingw 7.3.0 32-bit on Windows.
Thanks,
Alex
Re: QWebSocket closeCode not returning expected value
The problem was that I was using close codes 1000-1015 and QWebsocket appeared to be disagreeing with their usage and reported 1002, probably because they were being misused. The solution was to just use close codes in the 4000+ range.
Re: QWebSocket closeCode not returning expected value
Quote:
Originally Posted by
413X
The problem was that I was using close codes 1000-1015 and QWebsocket appeared to be disagreeing with their usage and reported 1002, probably because they were being misused. The solution was to just use close codes in the 4000+ range.
poppy playtime chapter 3
These are the real challenges developers face when dealing with WebSocket closures and the need for consistent error codes across their implementations.
Re: QWebSocket closeCode not returning expected value
There could be a discrepancy in how the server (uWS) and the client (Qt's QWebSocket) interpret the WebSocket close frames. Ensure that the server is sending a well-formed close frame according to the WebSocket RFC!
You can check the exact frame being sent by the server (using tools like Wireshark or the browser's WebSocket debugging tools) to ensure that the close code and reason are being sent correctly.
getaway shootout
You can try enabling debugging or logging for WebSocket frames to see if there are any anomalies in the connection close process. Check the QWebSocket debug output (or even modify the Qt source if you're building from source) to inspect the raw frames being received and processed by the client.