Module
whiplash/websocket/route
src/whiplash/websocket/route.pookaWebSocket routes: what a server does with a conversation once a client has upgraded, over HTTP/1.1, HTTP/2 (RFC 8441) or HTTP/3 (RFC 9220).
A Route answers each message with the messages to send back. PONGs and
close replies are automatic. with_open adds a function that runs beside
the conversation with a Pusher, for messages the server sends on its
own, such as a broadcast.
let echo = route.Route(fn(message) { [message] }, None, True)
API reference
class Transport(
read: fn(Int) -> Result(BitArray, String),
write: fn(BitArray) -> Result(Nil, String)
)
A conversation needs only ordered bytes in and out. HTTP/1 supplies a
socket; HTTP/2 supplies flow-controlled DATA and a bounded input channel.
wait reads with a deadline and passes on what came; over HTTP/1 a quiet
conversation hibernates meanwhile, so it must be the reader's last call.
read: fn(Int) -> Result(BitArray, String)
fn(Int) -> Result(BitArray, String)write: fn(BitArray) -> Result(Nil, String)
fn(BitArray) -> Result(Nil, String)
fn(fn(Int) -> Result(BitArray, String), fn(BitArray) -> Result(Nil, String)) -> whiplash/websocket/route.Transportwait(timeout: Int, resume: fn(Result(BitArray, String)) -> Nil) -> Nil
fn(Int, fn(Result(BitArray, String)) -> Nil) -> Nilclass Route(
on_message: fn(ws.Message) -> List(ws.Message),
protocol: Option(String),
compression: Bool
)
A WebSocket conversation: the reply to each message, the subprotocol to select (only if the client offered it), and whether to accept permessage-deflate when the client asks. Without an acceptable offer, messages go uncompressed.
on_message: fn(ws.Message) -> List(ws.Message)
fn(whiplash/websocket.Message) -> List(whiplash/websocket.Message)protocol: Option(String)
Option(String)compression: Bool
Bool
fn(fn(whiplash/websocket.Message) -> List(whiplash/websocket.Message), Option(String), Bool) -> whiplash/websocket/route.Routeopened() -> Option(fn(Pusher) -> Nil)
fn() -> Option(fn(whiplash/websocket/route.Pusher) -> Nil)The function to run beside the conversation, if any.
with_open(open: fn(Pusher) -> Nil) -> Route
fn(fn(whiplash/websocket/route.Pusher) -> Nil) -> whiplash/websocket/route.RouteRun open in a process linked to the conversation once the upgrade
succeeds. It receives a Pusher: messages that the server sends on its
own, such as a broadcast or a clock. Pushed messages are never
compressed (RFC 7692 lets any message go uncompressed), so they do not
disturb the conversation's compression context. Over HTTP/1 the pusher
writes to the socket; over HTTP/2 its frames join the stream's DATA.
class Pusher(post: fn(ws.Message) -> Result(Nil, String))
Sends text, binary and ping messages to one client. A failed send means the client has gone: drop the pusher. A message pushed while the closing handshake runs may still be written just before the Close frame.
post: fn(ws.Message) -> Result(Nil, String)
fn(whiplash/websocket.Message) -> Result(Nil, String)
fn(fn(whiplash/websocket.Message) -> Result(Nil, String)) -> whiplash/websocket/route.Pushersend(message: ws.Message) -> Result(Nil, String)
fn(whiplash/websocket.Message) -> Result(Nil, String)class Incoming
What an HTTP/2 conversation reads from its connection: the peer's bytes, or a frame the application pushed, to be written in turn.
class Input(bytes: BitArray) is Incoming
bytes: BitArray
BitArray
fn(BitArray) -> whiplash/websocket/route.Inputclass Pushed(frame: BitArray) is Incoming
frame: BitArray
BitArray
fn(BitArray) -> whiplash/websocket/route.Pushedpusher(write: fn(BitArray) -> Result(Nil, String)) -> Pusher
fn(fn(BitArray) -> Result(Nil, String)) -> whiplash/websocket/route.PusherA Pusher writing each message, as an uncompressed frame, through write.
serve(
client: socket.Socket,
method: String,
headers: List(#(String, String)),
bytes: BitArray,
route: Route,
max_message: Int,
timeout: Int
) -> Nil
fn(whiplash/socket.Socket, String, List(#(String, String)), BitArray, whiplash/websocket/route.Route, Int, Int) -> NilThe initial HTTP bytes have already been checked and bounded by HTTP/1.
connect_stream(
headers: List(#(String, String)),
route: Route,
read: fn(Int) -> Result(Incoming, String),
max_message: Int,
timeout: Int
) -> Result(body.Stream, ws.WebsocketError)
fn(List(#(String, String)), whiplash/websocket/route.Route, fn(Int) -> Result(whiplash/websocket/route.Incoming, String), Int, Int) -> Result(whiplash/body.Stream, whiplash/websocket.WebsocketError)An extended CONNECT answers with 200, then carries WebSocket frames in DATA. There are no HTTP/1 Upgrade/Connection/Accept fields (RFC 8441 4).