veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

net/websocket

lib/net/websocket.vk

lib/net/websocket.vk Pure-Veko RFC 6455 WebSocket upgrade handshake (Sec-WebSocket-Accept SHA-1 + Base64) and binary/text framing over io/Stream.

signature
OP-CONT = fn [] i64

WebSocket opcodes (RFC 6455 Section 5.2).

signature
OP-TEXT = fn [] i64
signature
OP-BINARY = fn [] i64
signature
OP-CLOSE = fn [] i64
signature
OP-PING = fn [] i64
signature
OP-PONG = fn [] i64
Frame struct
signature
Frame = struct { fin bool = ..., opcode i64, masked bool = ..., payload bytes }

Decoded WebSocket frame.

signature
accept-key = fn [client_key str] str

Computes the RFC 6455 Sec-WebSocket-Accept header value from client_key.

Executable Example
veko
use [ws = net/websocket]
acc = ws/accept-key 'dGhlIHNhbXBsZSBub25jZQ=='
if acc != 's3pPLMBiTxaQ9kYGzzhZRbK+xOo=' [ err 'ws accept-key' ] else [ .ok ]
signature
encode-frame = fn [f Frame, mask_key bytes] bytes

Encodes a WebSocket Frame into RFC 6455 wire bytes (applying 4-byte XOR mask_key when f.masked == true).

Executable Example
veko
use [ws = net/websocket]
f = ws/Frame { fin = true, opcode = ws/OP-TEXT, masked = true, payload = (bytes/from-str 'hello ws') }
wire = ws/encode-frame f (bytes/from-str '1234')
dec = ws/decode-frame wire
if dec.opcode != 1 or (bytes/to-str dec.payload) != 'hello ws' [ err 'ws frame' ] else [ .ok ]
signature
decode-frame = fn [wire bytes] Frame

Decodes a single RFC 6455 WebSocket Frame from wire bytes (unmasking payload automatically).

signature
write-frame = fn [mut s io/Stream, f Frame] i64

Writes a WebSocket Frame onto io/Stream s (generating fresh CSPRNG mask when f.masked == true).

signature
read-frame = fn [mut s io/Stream] Frame

Reads the next RFC 6455 WebSocket Frame from io/Stream s.

signature
client-handshake = fn [mut s io/Stream, host str, path str] enum

Performs an RFC 6455 client HTTP/1.1 WebSocket Upgrade handshake over io/Stream s.

signature
server-handshake = fn [mut s io/Stream] str

Performs an RFC 6455 server HTTP/1.1 WebSocket Upgrade handshake over io/Stream s and returns the request path.

FrameTry struct
signature
FrameTry = struct { complete bool, consumed i64, frame Frame }

Result of incremental non-blocking WebSocket frame parsing (try-decode-frame).

signature
try-decode-frame = fn [raw bytes] FrameTry

Incrementally decodes one RFC 6455 frame from a non-blocking receive buffer raw. Returns FrameTry { complete = false, consumed = 0, ... } if more bytes are needed.

Executable Example
veko
use [ws = net/websocket]
f = ws/Frame { fin = true, opcode = (ws/OP-TEXT), masked = false, payload = bytes/from-str 'async' }
wire = ws/encode-frame f (bytes/with-cap 0)
partial = ws/try-decode-frame wire[0..3]
full = ws/try-decode-frame wire
if partial.complete or (not full.complete) or (bytes/to-str full.frame.payload) != 'async' [ err 'ws try-decode' ] else [ .ok ]