std/websocket
On this page
Index
- type
Message - type
Kind - type
WsErr- method
WsErr.display
- method
- type
WsConn- method
WsConn.read - method
WsConn.write - method
WsConn.write_text - method
WsConn.ping - method
WsConn.close - method
WsConn.subprotocol - method
WsConn.set_deadline - method
WsConn.set_read_deadline - method
WsConn.set_write_deadline - method
WsConn.on_ping - method
WsConn.on_pong - method
WsConn.pong
- method
- fn
upgrade - fn
dial
Types
Message
struct Message {
kind: Kind
data: Bytes
}A whole message: its kind and data (a text message’s data is valid UTF-8).
Kind
enum Kind {
Text
Binary
}WsErr
enum WsErr {
Closed(code: Int, reason: Str)
Protocol(msg: Str)
TooBig(limit: Int)
Net(err: NetErr)
Handshake(status: Int, msg: Str)
}Why a connection stopped or an operation failed. Displays as
gorilla’s errors: websocket: close 1001 (going away): bye,
websocket: unknown opcode 3, websocket: read limit exceeded,
websocket: bad handshake.
WsErr.display
fn (e: WsErr) display() -> StrWsConn
struct WsConn {}A WebSocket connection, either side. One task may read while another writes; writes are whole messages, one at a time.
WsConn.read
fn (c: WsConn) read() -> Message ! WsErrThe next whole message. Pings are answered with pongs and pongs
skipped; a close frame is answered and is Closed.
WsConn.write
fn (c: WsConn) write(m: Message) -> () ! WsErrWsConn.write_text
fn (c: WsConn) write_text(s: Str) -> () ! WsErrWsConn.ping
fn (c: WsConn) ping(data: Bytes = b"") -> () ! WsErrWsConn.close
fn (c: WsConn) close(code: Int = 1000, reason: Str = "") -> () ! WsErrSends a close frame with code and reason (none for 1005) and closes the connection; later writes fail.
WsConn.subprotocol
fn (c: WsConn) subprotocol() -> StrThe subprotocol the server chose ("" for none).
WsConn.set_deadline
fn (c: WsConn) set_deadline(clock: Clock, t: Time?) uses clockDeadlines (decision 0430), gorilla’s SetReadDeadline and SetWriteDeadline: a read or write past its deadline fails with Net(Timeout), and the connection stays failed. None clears it.
WsConn.set_read_deadline
fn (c: WsConn) set_read_deadline(clock: Clock, t: Time?) uses clockWsConn.set_write_deadline
fn (c: WsConn) set_write_deadline(clock: Clock, t: Time?) uses clockWsConn.on_ping
fn (c: WsConn) on_ping[fx: Effects](f: fn(Bytes) -> () uses fx) uses fxgorilla’s SetPingHandler (decision 0430): f runs for each ping that
read() reads, in place of the automatic pong (send one with pong).
A later call replaces f. f runs inside read(), so it must not call
read().
WsConn.on_pong
fn (c: WsConn) on_pong[fx: Effects](f: fn(Bytes) -> () uses fx) uses fxgorilla’s SetPongHandler (decision 0430): f runs for each pong that read() reads (a keepalive moves the read deadline here).
WsConn.pong
fn (c: WsConn) pong(data: Bytes = b"") -> () ! WsErrA pong control frame, as gorilla’s WriteControl(PongMessage).
Functions
upgrade
fn upgrade[fx: Effects](req: http.Request, f: fn(WsConn) -> () uses fx, protocols: List[Str] = [], origins: List[Str] = [], max_message: Int = 1048576) -> http.Response uses fxThe response that upgrades req to a WebSocket and runs f with it (the
connection closes when f returns), as gorilla’s Upgrader: GET,
Connection: upgrade, Upgrade: websocket, Sec-WebSocket-Version: 13, a key of 16 bytes, and an Origin whose host is the request’s
(or one of origins; ["*"] allows any); else 400, 405, or 403 with
the status text. The first of protocols the client offers is the
subprotocol.
dial
fn dial(net: Net, url: Str, headers: Map[Str, Str] = {}, protocols: List[Str] = [], max_message: Int = 1048576, roots: Bytes = b"") -> WsConn ! WsErr uses net.dialA client connection to url (ws://host[:port]/path?query, or
wss:// over TLS through std/tls, verified against roots, a PEM
bundle, when given, else the system’s), as gorilla’s Dialer: the
handshake with headers and the protocols offered, the 101 and its
Sec-WebSocket-Accept checked.