Goal#
Expose the WebSocket protocol implementation to JavaScript via the standard WebSocket interface, wired into the main event loop and the Web Workers runtime.
Depends on: "Implement WebSocket protocol (RFC 6455) in we-net".
Scope#
- Constructor
new WebSocket(url, protocols?)whereprotocolsis a string or array of strings- URL must be
ws:orwss:; throwSyntaxErrorotherwise - Asynchronous connect — return immediately, dispatch
openorerrorlater via the event loop
- Properties
url,readyState(CONNECTING = 0,OPEN = 1,CLOSING = 2,CLOSED = 3)bufferedAmount(bytes queued but not yet sent — counts data frame payload bytes)protocol,extensions(populated from the handshake response)binaryType('blob'or'arraybuffer', default'blob')- Static constants
WebSocket.CONNECTINGetc.
- Methods
send(data)acceptingstring,ArrayBuffer,ArrayBufferView,Blob- Throws
InvalidStateErrorif called whileCONNECTING close(code?, reason?)— code must be 1000 or 3000–4999, reason ≤ 123 UTF-8 bytes
- Event handlers
onopen,onmessage,onerror,oncloseplusaddEventListenerequivalentsMessageEventdataisstring,ArrayBuffer, orBlobdepending onbinaryTypeCloseEventwithcode,reason,wasClean
- Event loop integration
- The main event loop polls the
we-netWebSocket channels each tick and dispatches events - In Web Workers (Phase 19), the worker's event loop does the same
- The main event loop polls the
- Lifecycle
- GC root the
WebSocketwhile it has an active connection or queued data, so a created-but-unreferenced WebSocket stays alive
- GC root the
Acceptance Criteria#
const ws = new WebSocket('wss://...'); ws.onopen = ...dispatchesopenafter the handshake completes- Sending strings,
ArrayBuffers, andUint8Arrays round-trips against an echo server binaryType = 'arraybuffer'causes inbound binary messages to be delivered asArrayBuffer(instead ofBlob)ws.close(1000, 'bye')triggers the close handshake and acloseevent withwasClean: true- A server-initiated close delivers a
closeevent with the server's status code and reason - WebSocket usable inside a
DedicatedWorkerGlobalScope - Memory: a
new WebSocket(url)without further references is not GC'd before the connection closes - Unit tests + an integration test that runs a tiny in-process WebSocket server (using the protocol crate's primitives) and verifies the JS side
- Conventions
cargo fmt --all --checkcargo clippy --workspace -- -D warningscargo test --workspace