WebSocket
represents an active WebSocket connection.
Handlers receive a reference to this and call WebSocket.send or
WebSocket.send-binary to queue outgoing text or binary
frames. The event loop drains the outbox after the handler returns.
The protocol field holds the negotiated subprotocol (RFC 6455 ยง4.2.2),
or Nothing if no subprotocol was negotiated.
encode-binary
(Fn [(Ref (Array Byte) a)] (Array Byte))
(encode-binary data)
encodes a byte array as a WebSocket binary frame (no mask).
encode-ping
(Fn [(Ref (Array Byte) a)] (Array Byte))
(encode-ping payload)
encodes a WebSocket ping frame with the given payload (may be empty).
encode-text
(Fn [(Ref String a)] (Array Byte))
(encode-text msg)
encodes a string as a WebSocket text frame (no mask).
outbox
(Fn [(Ref WebSocket a)] (Ref (Array (Array Byte)) a))
gets the outbox property of a WebSocket.
protocol
(Fn [(Ref WebSocket a)] (Ref (Maybe String) a))
gets the protocol property of a WebSocket.
send
(Fn [(Ref WebSocket a), (Ref String b)] ())
(send ws msg)
queues a text message for sending on the WebSocket connection.
send-binary
(Fn [(Ref WebSocket a), (Ref (Array Byte) b)] ())
(send-binary ws data)
queues a binary message for sending on the WebSocket connection.
send-binary-now
(Fn [(Ref WebSocket a), (Ref (Array Byte) b)] Bool)
(send-binary-now ws data)
sends a binary message immediately, bypassing the outbox.
Like send-now but for binary data.
send-now
(Fn [(Ref WebSocket a), (Ref String b)] Bool)
(send-now ws msg)
sends a text message immediately, bypassing the outbox. Writes the WebSocket frame directly to the socket, waiting for the socket to drain if the kernel send buffer is full. Use this inside handlers that block for a long time (e.g. LLM streaming) so that each message reaches the client without waiting for the handler to return.
Returns true when the whole frame was written. Returns false when the
connection is dead or the client stopped draining; the caller should stop
sending on this connection.
set-outbox
(Fn [WebSocket, (Array (Array Byte))] WebSocket)
sets the outbox property of a WebSocket.
set-outbox!
(Fn [(Ref WebSocket a), (Array (Array Byte))] ())
sets the outbox property of a WebSocket in place.
set-protocol
(Fn [WebSocket, (Maybe String)] WebSocket)
sets the protocol property of a WebSocket.
set-protocol!
(Fn [(Ref WebSocket a), (Maybe String)] ())
sets the protocol property of a WebSocket in place.
update-fd
(Fn [WebSocket, (Ref (Fn [Int] Int a) b)] WebSocket)
updates the fd property of a WebSocket using a function f.
update-outbox
(Fn [WebSocket, (Ref (Fn [(Array (Array Byte))] (Array (Array Byte)) a) b)] WebSocket)
updates the outbox property of a WebSocket using a function f.
update-protocol
(Fn [WebSocket, (Ref (Fn [(Maybe String)] (Maybe String) a) b)] WebSocket)
updates the protocol property of a WebSocket using a function f.