websocket #
The RFC 6455 WebSocket protocol for OCaml, without I/O, and an Eio client that
drives it.
A WebSocket connection starts as an HTTP/1.1 request that asks to upgrade,
then carries framed messages in both directions over the same TCP connection.
Beyond the frame encoding, an endpoint reassembles fragmented messages,
answers pings and runs a close handshake whose status codes RFC 6455 section
7.4 restricts. The nox-websocket library covers the whole protocol without
performing I/O, so any transport can drive it:
- the frame codec (
Websocket.encode,Websocket.decodeand the frame
constructors), with masking and fragmentation; Websocket.Handshake, which builds and checks the upgrade request and
response, includingSec-WebSocket-Accept, subprotocols and extensions.
The client sends a random base64Sec-WebSocket-Key, and the server answers
with base64(SHA-1(key || "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")), which
shows that the server understood the WebSocket upgrade and is not a plain
HTTP server replaying the request (RFC 6455, section 4.2.2);Websocket.State, which reassembles messages, answers control frames and
runs the close handshake;Websocket.Close_code, whose type admits only the codes a peer may send.
The nox-websocket-eio library runs a client over an Eio flow with
Websocket_eio.connect, send, recv and close. The server side of the
upgrade is driven by the HTTP server that owns the listener, such as the
respond package's WebSocket route.
Installation #
$ opam install nox-websocket
If opam cannot find the package, it has not reached the public
opam-repository yet. Add the overlay repository first:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-websocket
Usage #
Encode a text frame. Frames are masked by default, as RFC 6455 requires of a
client: the client XORs the payload with a fresh random 4-byte key that it sends
in the frame header. Masking hides nothing from the server; it stops a script
in a browser from choosing the exact bytes on the wire, which could make a
confused intermediary proxy read them as an HTTP request and poison its cache
(RFC 6455, sections 5.3 and 10.3). The mask therefore comes from a
cryptographic random number generator, so a client installs the default
generator once at startup. The 11 bytes of "hello" are two header bytes (FIN
and the text opcode, then the mask bit and the 7-bit length 5), the four bytes
of the masking key and the five masked payload bytes:
# Crypto_rng_unix.use_default ()
- : unit = ()
# let frame = Websocket.text "hello" in
let bytes = Websocket.encode frame in
String.length bytes
- : int = 11
Decode a frame from a buffer. The decoder returns the frame and any trailing
bytes, or says that it needs more input:
# let bytes = Websocket.encode ~mask:false (Websocket.text "hello") in
match Websocket.decode bytes with
| Ok ({ opcode; payload; fin }, rest) ->
Fmt.str "opcode=%a payload=%S fin=%b rest=%d"
Websocket.pp_opcode opcode payload fin (String.length rest)
| Error `Need_more -> "need more"
| Error (`Invalid m) -> "invalid: " ^ m
- : string = "opcode=text payload=\"hello\" fin=true rest=0"
Need_more is the normal case while streaming. Keep reading from the socket
and decode again until a whole frame has arrived:
# let full = Websocket.encode ~mask:false (Websocket.binary "\x01\x02\x03") in
let partial = String.sub full 0 (String.length full - 1) in
Websocket.decode partial
- : (Websocket.t * string, [ `Invalid of string | `Need_more ]) result =
Error `Need_more
Build the common control frames directly:
# let p = Websocket.ping "keepalive"
and c = Websocket.close ~reason:"normal" Websocket.Close_code.normal in
(p.opcode, c.opcode)
- : Websocket.opcode * Websocket.opcode = (Websocket.Ping, Websocket.Close)
License #
ISC. See LICENSE.md.