CX · Reference guidews
PlaygroundDownloadsAboutv0.18.0-pre.1

ws

The RFC 6455 WebSocket frame codec: octets and values in, octets and values out. One frame is one element and decode produces exactly what encode consumes plus the octet count it took, so a caller reads a buffer incrementally as a VALUE rather than through a handle. It masks and unmasks, fragments and assembles, builds and reads the three control frames, reads a close body into its code and reason, answers a text message's payload only when it is valid UTF-8, and derives the handshake's accept value. Every def is pure and Ring 1: the connection, the upgrade exchange and the heartbeat cadence are not this module's.

ws:encode

[$ws:encode] -> bytes — The wire octets of one frame, masked when key= is present, in the minimal length form for its payload.

ws:decode

[$ws:decode] -> element — One frame with the octet count it consumed and its payload UNMASKED; refuses a non-minimal length form, an unassigned opcode, a reserved bit no extension negotiated, the role rule's masking violation, a fragmented control frame, a payload over the bound and a buffer shorter than the header declares.

ws:decode-all

[$ws:decode-all] -> element — The frames a buffer carries, in order, with the trailing octets it could not complete as a [remainder bytes=N] — the incremental contract expressed as a value rather than as a handle.

ws:mask

[$ws:mask] -> bytes — The payload XORed with the repeating four-octet key; its own inverse, and a key that is not four octets is a refusal.

ws:fragment

[$ws:fragment] -> element — Split a message at the bound — the first frame carries the message's opcode with FIN clear, every later one :continuation, the last FIN set — and a message under the bound yields ONE frame with FIN set, never one frame plus an empty terminator.

ws:assemble

[$ws:assemble] -> element — One message from a frame sequence; a control frame between fragments is admitted and does not join the message, while a continuation with no start, a data frame inside a fragmented message, a fragment count over the bound and a message over the byte bound are refusals.

ws:control

[$ws:control] -> element — A :ping, :pong or :close frame carrying at most 125 octets — which is why the control form needs no extended length field at all.

ws:close

[$ws:close] -> element — The close frame for a code and a reason; the admitted set is RFC 6455 §7.4.1's nine sendable codes plus the private range 3000–4999, and 1005 and 1006 are refused on the wire because they are what an API reports.

ws:close-of

[$ws:close-of] -> element — A close frame's code and reason, code=:absent for an empty body; half a code, a code outside the admitted set and a reason that is not UTF-8 are refusals.

ws:text-of

[$ws:text-of] -> string — A text message's payload as a string, refusing when the ASSEMBLED payload is not valid UTF-8 — a lone surrogate included, and the rule is the message's rather than the frame's.

ws:accept

[$ws:accept] -> string — The Sec-WebSocket-Accept for a client key: base64 of SHA-1 over the key and the protocol's fixed GUID, refusing a key that is not a base64-encoded sixteen-octet nonce.