WebSocket Client
std.websocket provides a WS/WSS client. client.norm defines public types, parameters, and defaults.
Connect with connectWebSocket, reusing std.http.Uri, HeaderMap, std.io.Bytes, and std.time.Duration. sendText and sendBinary send complete messages; receive returns a WebSocketEvent. Asynchronous calls use the existing Task; do not wait for network operations on a UI thread.
Lifecycle and flow
- Only one receive operation may be active per connection. Concurrent sends are serialized, and time spent waiting to send counts toward the operation timeout.
- The receive side assembles protocol fragments and reads the next event on demand. A complete message is bounded by
maximumMessageBytes. Applications should keep receiving; an unconsumed event pauses delivery of later data and control events. - The transport responds to Ping automatically. The response to an active
pingis delivered as a Pong event; successful sending of Ping does not mean Pong has arrived. - A receive timeout or cancellation does not consume the next message. A send that has started releases the connection on timeout or cancellation; its outcome cannot be treated as unexecuted.
finishstarts the close handshake, discards subsequent data, and waits within the budget for the peer to close.closereleases the connection immediately foruse, cancellation, and execution-scope cleanup; repeated closing does not release twice.- A normal peer close returns a Closed event with status and reason. Connection, TLS, handshake, protocol, and capacity errors use
WebSocketException.
The client does not reconnect automatically or replay messages. No WebSocket server is provided.
Implementation and verification
| Responsibility | Entry point |
|---|---|
| Platform contract | websocket |
| JDK transport | JdkWebSocketTransport, JdkWebSocketConnection |
| ABI | stdlib-abi.json |
| Real network and TLS tests | JdkWebSocketTest |
| Norm calls and resource lifecycle | WebSocketClientTest |