Spindle_http.WebsocketWebSockets, served and called, in one model: a protocol says what each side sends, and each end holds a socket typed from its own side.
type said = { text : string } [@@deriving wiretype]
let chat =
Websocket.(protocol ~client:(json said_json) ~server:(json said_json) ())
let echo =
Spindle.get path (Returns.websocket chat)
(Dep.return
(Ok
(fun ws ->
let rec echo () =
let* said = Websocket.receive ws in
let* () = Websocket.send ws said in
echo ()
in
echo ())))
let greet client =
Spindle_client.websocket client chat "wss://example.com/echo" (fun ws ->
let* () = Websocket.send ws { text = "hi" } in
Websocket.receive ws)The framework runs RFC 6455 whole -- the frames, their masks and fragments, ping and pong, keep-alive, the closing handshake -- and a handler only receives, sends and, if it wants to, closes. Every failure is a value: a handler returns a result, so a loop written with let* ends at the first thing that went wrong and says which, and how it ended decides the close. Nothing here raises into the application, and nothing cancels a handler whose socket has gone: it learns at its next receive or send.
val json : 'a Wiretype.t -> 'a messageval text : string messageval binary : string messageval protocol :
?subprotocol:string ->
client:'c message ->
server:'s message ->
unit ->
('c, 's) protocolsubprotocol is Sec-WebSocket-Protocol, which both ends then insist on: a server refuses a client that did not offer it, and a client one that did not choose it.
val subprotocol : (_, _) protocol -> string optionAn open socket, from one end: it receives 'i and sends 'o. A route's socket is ('c, 's) t, a caller's ('s, 'c) t. It belongs to the fibers of the domain that runs it.
type close_code = | Normal1000
*)| Going_away1001: a server stopping, a page closed
*)| Protocol_error1002
*)| Unsupported1003: a message of a kind this end does not take
*)| Invalid_data1007: a message that could not be read
*)| Policy1008
*)| Too_big1009
*)| Internal1011: our bug
*)| Code of intany other a peer may send -- 1005 for a close that named none, 1010, 1012 to 1014, and 3000 to 4999, the application's own
*)Why a socket is closed, as RFC 6455 §7.4 numbers them.
A close, by either end: its code and reason.
type error = [ | `Socket of [ `Closed of close ]closed, by either end: the socket's own state, under its name
*)| `Lost of stringthe connection ended without a close, or went silent, or stopped taking what was written: in words for the log
*)| `Undecodable of stringa message its description could not read, in words for the log; the socket is still open
*)| `Unwritable of stringa message its description could not write -- a value its JSON description cannot encode, text that is not UTF-8 (§8.1) -- in words for the log; nothing was sent, and the socket is still open
*) ]How a socket's work failed, in the words every Hypha library uses, so a handler's let* runs over a socket and a statement alike: what a call answers is [> error], open to the handler's own.
val error_to_string : [< error ] -> stringThe next whole message, waiting for it. Once the socket has ended, its ending, every time.
Answers once the message is written. A peer that takes nothing written for the connection's send_timeout makes it `Lost; one still taking it, however slowly, is sent the whole. A value its description cannot write -- one JSON cannot encode, text that is not UTF-8 -- is `Unwritable, and nothing is sent. A send cancelled -- by a timeout, or an Eio.Fiber.first it lost -- may have written part of its frame, which the peer would read the next frame into, so it ends the socket as `Lost and the cancellation goes on: every later send answers `Lost, and nothing more is written.
A message encoded once, to be sent to many sockets: what a Spindle.Broadcast of sockets carries, so the publisher renders once.
A value its description cannot write makes a message every send of which answers `Unwritable.
val close : ?code:close_code -> ?reason:string -> (_, _) t -> unitBegins the closing handshake with code (Normal) and reason (none). What follows is read as ever, and receive answers `Socket (`Closed _) once the peer has answered. A code no endpoint may send is our bug: logged, and the socket closed with Internal. So is a reason longer than 123 bytes or not UTF-8, which no close can carry (§5.5, §8.1): logged, and the close sent without it.