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.

copy

instantiate

(Fn [(Ref WebSocket a)] WebSocket)

copies a WebSocket.

delete

instantiate

(Fn [WebSocket] ())

deletes a WebSocket. Should usually not be called manually.

encode-binary

defn

(Fn [(Ref (Array Byte) a)] (Array Byte))

                        (encode-binary data)
                    

encodes a byte array as a WebSocket binary frame (no mask).

encode-ping

defn

(Fn [(Ref (Array Byte) a)] (Array Byte))

                        (encode-ping payload)
                    

encodes a WebSocket ping frame with the given payload (may be empty).

encode-text

defn

(Fn [(Ref String a)] (Array Byte))

                        (encode-text msg)
                    

encodes a string as a WebSocket text frame (no mask).

fd

instantiate

(Fn [(Ref WebSocket a)] (Ref Int a))

gets the fd property of a WebSocket.

init

instantiate

(Fn [Int, (Array (Array Byte))] WebSocket)

creates a WebSocket.

outbox

instantiate

(Fn [(Ref WebSocket a)] (Ref (Array (Array Byte)) a))

gets the outbox property of a WebSocket.

prn

instantiate

(Fn [(Ref WebSocket a)] String)

converts a WebSocket to a string.

send

defn

(Fn [(Ref WebSocket a), (Ref String b)] ())

                        (send ws msg)
                    

queues a text message for sending on the WebSocket connection.

send-binary

defn

(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

defn

(Fn [(Ref WebSocket a), (Ref (Array Byte) b)] ())

                        (send-binary-now ws data)
                    

sends a binary message immediately, bypassing the outbox. Like send-now but for binary data.

send-now

defn

(Fn [(Ref WebSocket a), (Ref String b)] ())

                        (send-now ws msg)
                    

sends a text message immediately, bypassing the outbox. Writes the WebSocket frame directly to the socket. 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.

set-fd

instantiate

(Fn [WebSocket, Int] WebSocket)

sets the fd property of a WebSocket.

set-fd!

instantiate

(Fn [(Ref WebSocket a), Int] ())

sets the fd property of a WebSocket in place.

set-outbox

instantiate

(Fn [WebSocket, (Array (Array Byte))] WebSocket)

sets the outbox property of a WebSocket.

set-outbox!

instantiate

(Fn [(Ref WebSocket a), (Array (Array Byte))] ())

sets the outbox property of a WebSocket in place.

str

instantiate

(Fn [(Ref WebSocket a)] String)

converts a WebSocket to a string.

update-fd

instantiate

(Fn [WebSocket, (Ref (Fn [Int] Int a) b)] WebSocket)

updates the fd property of a WebSocket using a function f.

update-outbox

instantiate

(Fn [WebSocket, (Ref (Fn [(Array (Array Byte))] (Array (Array Byte)) a) b)] WebSocket)

updates the outbox property of a WebSocket using a function f.