std/ws
WebSocket connections (RFC 6455), both sides: wsAccept upgrades a TCP connection a server has already accepted, and wsConnect / wsConnectTls dial one. WsConn.recv reassembles fragmented messages and answers pings itself.
from "std/ws" import { WsConn, WsMessage, wsAccept, wsConnect, wsConnectTls }API reference
WsConn
pub struct WsConnWsConn.close
fn WsConn.close(self: &mut WsConn): voidSend a close frame and release the socket and TLS state now rather than at drop. Afterwards every send and recv fails. The handles are reset, not just flagged: this used to close the fd and keep the number, so a later send wrote to whatever the kernel reused it for, and the TLS state leaked because drop skips a closed conn.
WsConn.ping
fn WsConn.ping(self: &WsConn): Result<i32, string>Send a ping.
WsConn.readMessage
fn WsConn.readMessage(self: &WsConn, maxBytes: i64, returnPongs: bool): Result<WsMessage, string>WsConn.recv
fn WsConn.recv(self: &WsConn): Result<WsMessage, string>Read next WebSocket message. Handles fragmentation, responds to ping automatically. &Self, like the sends: a read changes nothing in the handle, so one task can read while scoped tasks borrowing the same connection write (Task.scope).
WsConn.recvWithin
fn WsConn.recvWithin(self: &WsConn, maxBytes: i64): Result<WsMessage, string>recv for a server facing untrusted peers. A message longer than maxBytes is refused before its payload is allocated (close 1009, Err "message too big"): the length is the peer's to claim. Pongs come back as messages, so a heartbeat can tell a live peer from a half-open socket; one arriving mid-fragment is dropped.
WsConn.sendBinary
fn WsConn.sendBinary(self: &WsConn, data: &string): Result<i32, string>Send a binary message.
WsConn.sendSealed
fn WsConn.sendSealed(self: &WsConn, msg: &Sealed): Result<i32, string>Send a sealed buffer as one text message, framed straight from its bytes.
WsConn.sendShared
fn WsConn.sendShared(self: &WsConn, msg: &Shared): Result<i32, string>Send a shared sealed buffer as one text message. The fan-out shape: seal and share a message once, hand each connection's writer a refcount clone, and every socket is written from the one buffer.
WsConn.sendText
fn WsConn.sendText(self: &WsConn, msg: &string): Result<i32, string>Send a text message.
WsConn.tlsHandle
fn WsConn.tlsHandle(self: &WsConn): i64The TLS handle, 0 for plain TCP. Opaque to callers: pass it back to std, never construct one.
WsConn.view
fn WsConn.view(fd: i32, ssl: i64, isClient: bool): WsConnA second handle over a socket some other WsConn owns: same fd and TLS handle, but this one never closes them (ws.rawFd() / ws.tlsHandle() are the values to pass). The owner must outlive it, and nothing checks that, hence @unsafe. For a reader and a writer on one connection use Task.scope, whose tasks borrow the owner.
WsMessage
pub struct WsMessageFields: opcode: WsOpcode, payload: string.
WsOpcode
pub enum WsOpcode: i32RFC 6455 §5.2 opcode nibble. The discriminant IS the wire value, so op as i32 is what goes in the frame header and WsOpcode.tryFrom(nibble) rejects the reserved codes (3–7, 11–15) that the spec says must fail the connection.
Variants:
Continuation = 0Text = 1Binary = 2Close = 8Ping = 9Pong = 10
Functions
extractWsKey
pub fn extractWsKey(raw: &string): stringExtract Sec-WebSocket-Key from raw HTTP request.
isWsUpgrade
pub fn isWsUpgrade(raw: &string): boolCheck if raw HTTP request bytes contain a WebSocket upgrade request.
wsAccept
pub fn wsAccept(conn: TcpStream, rawRequest: &string): Result<WsConn, string>Accept a WebSocket upgrade on an already-accepted TCP connection, which the WsConn takes over (on error it is dropped, closing the socket). Pass the raw HTTP request bytes so the handshake can be completed.
wsConnect
pub fn wsConnect(ip: u32, port: u16, path: &string): Result<WsConn, string>Connect to a WebSocket server. Performs TCP connect + HTTP upgrade handshake. Returns a WsConn on successful 101 response.
wsConnectTls
pub fn wsConnectTls(ip: u32, port: u16, hostname: &string, path: &string): Result<WsConn, string>Connect to a WebSocket server over TLS (wss://). Performs TCP connect, TLS handshake (blocking — call before setting fds nonblocking), then the HTTP upgrade handshake over the encrypted channel. hostname is used for SNI, certificate validation, and the Host header.