websocket API reference
An RFC 6455 WebSocket client over net. connect performs the HTTP Upgrade handshake (verifying the server's Sec-WebSocket-Accept, which is SHA-1 + base64 of the client key), then send / sendBytes write masked text / binary frames and receive reads the next message, transparently answering pings with pongs and reassembling fragmented messages. close sends a close frame and shuts the socket. ws:// is plain TCP, wss:// is TLS.
connectOptions takes an Options: extra handshake headers (an API key or a token), the subprotocols to offer (graphql-transport-ws), and TLS trust for a self-signed or private-CA server. The subprotocol the server chose is Conn.protocol.
Client-to-server frames are masked (required by the spec); server frames are not. Needs the default jennifer binary (net); a protocol error throws Error{kind: "websocket"}, a connection dropped mid-frame kind "closed". A server-side upgrade is out of scope (it needs an httpd connection-hijack hook).
Import with import "websocket.j" as websocket;. See the websocket guide for prose and examples.
Contents
| Name | What it is |
|---|---|
close | Send a close frame and shut the connection. |
connect | Open a WebSocket connection to a ws:// or wss:// URL (default receive timeout). |
connectOptions | Open a WebSocket connection with Options: extra handshake headers, the subprotocols to offer, TLS trust a... |
connectWith | Open a WebSocket connection with an explicit per-receive read timeout. |
defaultOptions | The zero Options: what connect does. |
ping | Send a ping (empty payload). |
receive | Receive the next message. |
send | Send a text message. |
sendBytes | Send a binary message. |
Conn | struct: An open WebSocket connection. |
Message | struct: A received message. |
Options | struct: How connectOptions opens a connection. |
Functions
close
websocket.close(c as Conn)Send a close frame and shut the connection.
c- the connection
connect
websocket.connect(url as string)Open a WebSocket connection to a ws:// or wss:// URL (default receive timeout).
url- the ws:// or wss:// URL
- Returns
Conn - the open connection
- Throws
Error - kind "websocket" on a failed handshake
connectOptions
websocket.connectOptions(url as string, options as Options)Open a WebSocket connection with Options: extra handshake headers, the subprotocols to offer, TLS trust and the receive timeout. The subprotocol the server picked is protocol on the returned Conn ("" when it picked none; whether that is acceptable is the caller's call).
url- the ws:// or wss:// URL
options- how to connect
- Returns
Conn - the open connection
- Throws
Error - kind "websocket" on a bad URL or option, or a failed handshake
connectWith
websocket.connectWith(url as string, timeoutMs as int)Open a WebSocket connection with an explicit per-receive read timeout.
url- the ws:// or wss:// URL
timeoutMs- the per-receive read timeout in milliseconds
- Returns
Conn - the open connection
- Throws
Error - kind "websocket" on a failed handshake
defaultOptions
websocket.defaultOptions()The zero Options: what connect does. Set fields on the result.
- Returns
Options - the default options
ping
websocket.ping(c as Conn)Send a ping (empty payload).
c- the connection
receive
websocket.receive(c as Conn)Receive the next message. Pings are answered with pongs and skipped; fragmented text / binary messages are reassembled. A close frame is returned as kind "close".
c- the connection
- Returns
Message - the next message
- Throws
Error - kind "websocket" on a protocol error, "closed" for a connection dropped mid-frame
send
websocket.send(c as Conn, text as string)Send a text message.
c- the connection
text- the message text
sendBytes
websocket.sendBytes(c as Conn, data as bytes)Send a binary message.
c- the connection
data- the message payload
Structs
Conn
An open WebSocket connection.
def struct Conn {
socket as net.Conn,
timeoutMs as int,
protocol as string
};socket- the underlying (plain or TLS) connection
timeoutMs- the per-receive read timeout in milliseconds
protocol- the subprotocol the server chose from those offered, "" for none
Message
A received message.
def struct Message {
kind as string,
text as string,
data as bytes
};kind- "text", "binary", "close", or "pong"
text- the decoded text (for a "text" message; "" otherwise)
data- the raw payload bytes
Options
How connectOptions opens a connection. The zero value (from websocket.defaultOptions()) is what connect does: the 30 s receive timeout, no extra headers, no subprotocol, and TLS verified against the system roots.
def struct Options {
timeoutMs as int,
headers as map of string to string,
protocols as list of string,
caCert as bytes,
skipVerify as bool
};timeoutMs- the per-receive read timeout in milliseconds (0 = 30 s)
headers- extra headers for the handshake request, such as an API key
protocols- the subprotocols to offer, in order of preference (
Sec-WebSocket-Protocol) caCert- a PEM certificate to trust besides the system roots, for a private CA or a self-signed
wss://server skipVerify- accept any certificate; for a trusted LAN endpoint only, it exposes the connection to a man-in-the-middle