Skip to main content

Module ws

Module ws 

Source
Expand description

Minimal WebSocket server (RFC 6455 subset) with JSON envelopes.

Reference infrastructure for the web contributor. Speaks unfragmented text frames only (client frames masked, server frames unmasked) and maps JSON messages onto the config surface:

{"type": "config.get", "id": "1"}
{"type": "config.set", "id": "2", "config": {"led_brightness": 128}}
{"type": "system.get", "id": "3"}
{"type": "ping", "id": "4"}
{"type": "reset", "id": "5"}

Replies: {"type": "ok", "id": …, "config"?| "system"? …}, {"type": "pong", "id": …}, {"type": "err", "id": …, "code", "message"}. config.set takes a ConfigPatch — same merge semantics as POST /api/config. Fragmented frames, continuations and binary messages end the session without a close frame (extending that to a close code like 1003 is the contributor’s call).

Structs§

ConfigSetPayload 🔒
Payload of a config.set message.
MessageHead 🔒
First-pass envelope: {"type": "...", "id": "..."} (extra fields ignored).
Reply 🔒
Server reply envelope — flat struct (same JSON as a tagged enum).
SystemInfo 🔒
Runtime information payload (system.get).

Constants§

GUID 🔒
RFC 6455 magic GUID for the accept key.
IDLE_TIMEOUT 🔒
Per-frame idle budget — dead peers free their slot.
MAX_PAYLOAD 🔒
Largest client payload accepted (config patches are well below this).
MAX_REPLY 🔒
Largest server payload emitted (config JSON answers).
OPCODE_CLOSE 🔒
Frame opcode: close the connection.
OPCODE_PING 🔒
Frame opcode: ping probe.
OPCODE_PONG 🔒
Frame opcode: pong answer.
OPCODE_TEXT 🔒
Frame opcode: text payload.

Functions§

format_mac 🔒
Format MAC bytes as AA:BB:CC:DD:EE:FF.
handle_message 🔒
Parse and execute one JSON message, then send the reply.
handshake 🔒
Complete the HTTP→WebSocket upgrade handshake.
read_frame 🔒
Read one complete client frame (masked, unfragmented) into buf.
send_reply 🔒
Serialize and write one reply frame.
serve 🔒
Run one WebSocket session until close; the socket stays borrowed in.
write_frame 🔒
Write one unmasked server frame.