Skip to content

Expose the peer close code in the WebSocketClient onclose callback - #932

Open
lizezhong233 wants to merge 1 commit into
ithewei:masterfrom
lizezhong233:websocket-client-close-code-553
Open

lizezhong233 wants to merge 1 commit into
ithewei:masterfrom
lizezhong233:websocket-client-close-code-553

Conversation

@lizezhong233

Copy link
Copy Markdown

Closes #553.

The C++ client already echoes a peer's WebSocket CLOSE payload and invokes onclose() when the transport disconnects. With this change, applications can call getCloseCode() inside that same callback. A nonnegative result is the peer's 16-bit status code in host byte order.

Other result, under WebSocketClient::CloseCode Meaning in onclose()
NoCloseStatus The peer sent a CLOSE without a complete status code, including an empty CLOSE.
LocalClose The application closed the client before it received a peer CLOSE.
NoCloseFrame The transport disconnected or the connection attempt failed without a peer CLOSE.

The client records the code on the connection before echoing the original payload. It clears the result after the disconnect callback, including when the next attempt fails before the WebSocket handshake. The callback signature and timing stay the same. Frame validation continues to use the existing parser.

Compatibility

Existing zero-argument callbacks compile as written. The additional member changes the layout of the exported C++ WebSocketChannel class. Consumers of the shared C++ library should rebuild against the matching headers.

Tests

  • Built with GCC 14.2 in C++11 mode on Debian 13 x86_64, using both GNU Make and CMake. The new loopback test passed 50 consecutive Make-built runs and also passed in the CMake Release build. The existing parser assertions passed against both libraries, including a run with assertions explicitly enabled against the CMake Release library.
  • Covered an application code with a reason, an empty CLOSE, transport loss, local close, automatic reconnect and a refused retry. Also covered closing in a TEXT callback when the same read contains a following CLOSE.
  • Verified interoperability with Python websockets 16.0 on loopback. The peer received the same code and UTF-8 reason, and an empty CLOSE reply for its empty CLOSE. The two-scenario run passed 20 consecutive repeats.
  • Built and ran the unmodified examples/websocket_client_test.cpp against both the baseline and the updated library. Both runs produced the same callbacks and echoed 1000 / done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handling WebSocketClient's WS_OPCODE_CLOSE code

1 participant