WebSocket Wire Format (std/wsframe)
from "std/wsframe" import { computeAcceptKey, encodeWsFrame, parseWsFrame }The WebSocket wire format from RFC 6455: the handshake's accept key and the frame codec, with no IO. std/http's ws routes and std/ws both use it. It is a separate module so a plain HTTP server can speak WebSocket without linking OpenSSL, which std/ws pulls in for its TLS client.
Most programs want a ws route in std/http or wsConnect in std/ws; reach for this module only to frame bytes yourself.
API reference
WsFrame
pub struct WsFrameOne decoded frame, payload unmasked. size is how many bytes of the buffer it used.
Fields: opcode: i32, fin: bool, masked: bool, payload: string, size: i64.
WsFrameParse
pub enum WsFrameParseVariants:
Incomplete: The buffer holds only part of a frame.Frame(WsFrame)TooBig: The header claims a payload over the limit; nothing past the header was read.Invalid: A reserved opcode, a reserved bit set (no extension was negotiated), or a fragmented or oversized control frame: the connection must fail (close 1002).
Functions
computeAcceptKey
pub fn computeAcceptKey(clientKey: &string): stringThe Sec-WebSocket-Accept value answering a client's Sec-WebSocket-Key (RFC 6455 4.2.2).
encodeWsFrame
pub fn encodeWsFrame(opcode: i32, payload: &string, fin: bool, mask: &string): stringThe bytes of one frame. mask is "" for a server frame and the 4-byte key for a client frame (RFC 6455 5.3: client frames must be masked, server frames must not).
parseWsFrame
pub fn parseWsFrame(buf: &string, maxPayload: i64): WsFrameParseDecode the frame at the start of buf, refusing a payload longer than maxPayload before any of it is buffered: the length is the peer's to claim.