diff options
author | Clombrong <cromblong@egregore.fun> | 2025-08-17 16:40:13 +0200 |
---|---|---|
committer | Clombrong <cromblong@egregore.fun> | 2025-08-17 16:48:22 +0200 |
commit | 71e670133302f056928cc6f3a56068852b2808cf (patch) | |
tree | 261f7bde5e1b372e3fd30c05f50723a2108f0d84 | |
parent | 8d5c6c404122070f1a69f1d886f4729fdcf7e6dd (diff) |
refactor(portal-ws): recycle connect into portal_of_socket
-rw-r--r-- | portal/ws/portal.ml | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/portal/ws/portal.ml b/portal/ws/portal.ml index 06c9c6f..6814e7c 100644 --- a/portal/ws/portal.ml +++ b/portal/ws/portal.ml @@ -128,22 +128,22 @@ let ws_stream (url : string) = ws##.onopen := Dom.handler (fun _ -> signal is_open (); Js._false); let+ () = wait is_open in stream, push, ws -(** [connect domain] is an Lwt stream (and its push function) communicating with the - XMPP server running at [domain] via the Websocket subprotocol. +(** [portal_of_socket] is a Portal communicating with the socket [socket] via the + Websocket subprotocol. - This function is a complex wrapper around ws_stream, that accepts Markup.ml - signals and sends framed XML stanzas to the underlying socket, with exactly one - stanza per frame, according to RFC 7935. - In essence, it (tries to) expose an identical interface to the original XMPP - streamed protocol. + This function is a complex wrapper that accepts Markup.ml signals and sends framed + XML stanzas to the underlying socket, with exactly one stanza per frame, according + to RFC 7935. + + In essence, it exposes an identical interface to the original XMPP streamed + protocol. Here's an ASCII rendered flow of the data through the various streams. / -> push -> mu_stream -> to_frames -> ws_push -> \ function user websocket \ <--- stream <--- markup_lwt <--- ws_stream <--- / *) -let connect domain = - let+ ws_stream, ws_push, ws = ws_endpoint domain >>= ws_stream in +let portal_of_socket (ws : socket) : t = let open Markup_lwt in (* When sending a malformed stanza (one that Markup.ml doesn't like), a MalformedStanza exception is raised. *) @@ -153,7 +153,7 @@ let connect domain = (* Consumes a stream of Markup.ml signals into a series of frames sent to the WebSocket. *) let fragment_stream, fragment_push = Lwt_stream.create () in - let stream = ws_stream |> lwt_stream |> strings_to_bytes |> parse_xml ~report |> signals + let stream = ws.stream |> lwt_stream |> strings_to_bytes |> parse_xml ~report |> signals in let push = function | Some fs -> Markup.iter (fun f -> fragment_push (Some f)) fs @@ -176,8 +176,8 @@ let connect domain = |> elements (fun _ _ -> true) |> map (fun x -> write_xml x |> to_string) |> to_lwt_stream - |> Lwt_stream.iter (fun s -> ws_push (Some s))); - {domain; stream; push; _socket=ws} + |> Lwt_stream.iter (fun s -> ws.push (Some s))); + {stream; push; _socket=ws} let starttls _ = Lwt.fail_with "STARTTLS is unimplemented in WebSockets." |