we (web engine): Experimental web browser project to understand the limits of Claude
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement WebSocket JS API (new WebSocket, send, close, events) #187

open opened by pierrelf.com

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?) where protocols is a string or array of strings
    • URL must be ws: or wss:; throw SyntaxError otherwise
    • Asynchronous connect — return immediately, dispatch open or error later 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.CONNECTING etc.
  • Methods
    • send(data) accepting string, ArrayBuffer, ArrayBufferView, Blob
    • Throws InvalidStateError if called while CONNECTING
    • close(code?, reason?) — code must be 1000 or 3000–4999, reason ≤ 123 UTF-8 bytes
  • Event handlers
    • onopen, onmessage, onerror, onclose plus addEventListener equivalents
    • MessageEvent data is string, ArrayBuffer, or Blob depending on binaryType
    • CloseEvent with code, reason, wasClean
  • Event loop integration
    • The main event loop polls the we-net WebSocket channels each tick and dispatches events
    • In Web Workers (Phase 19), the worker's event loop does the same
  • Lifecycle
    • GC root the WebSocket while it has an active connection or queued data, so a created-but-unreferenced WebSocket stays alive

Acceptance Criteria#

  • const ws = new WebSocket('wss://...'); ws.onopen = ... dispatches open after the handshake completes
  • Sending strings, ArrayBuffers, and Uint8Arrays round-trips against an echo server
  • binaryType = 'arraybuffer' causes inbound binary messages to be delivered as ArrayBuffer (instead of Blob)
  • ws.close(1000, 'bye') triggers the close handshake and a close event with wasClean: true
  • A server-initiated close delivers a close event 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 --check
    • cargo clippy --workspace -- -D warnings
    • cargo test --workspace
sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mly7rphbar2w