blob: fa479653186cee4c31cb827afbd0dbd33a6a6d17 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
open Lwt.Syntax
open Js_of_ocaml
(* https://stackoverflow.com/questions/34929382/what-are-the-differences-between-lwt-async-and-lwt-main-run-on-ocaml-node-js *)
let rec run t =
let next_tick (_callback : unit -> unit) =
Js.Unsafe.(fun_call
(js_expr "process.nextTick")
[| inject (Js.wrap_callback _callback) |])
in Lwt.wakeup_paused ();
match Lwt.poll t with
| Some x -> x
| None ->
if Lwt.paused_count () > 0
then next_tick (fun () -> run t)
else ()
let () =
run @@
let* server = Portal_ws.ws_endpoint "telepath.im" in
let stream, push =
(* Echo is a websocket that... echoes you stuff. *)
Portal_ws.ws_stream "wss://echo.websocket.org" in
push (Some "great text");
push (Some "other text");
push (Some "yet another text");
push (Some "BYE");
let+ _ = Lwt_stream.iter
(fun greetings ->
match greetings with
(* When the websocket sends "BYE", we close. *)
| "BYE" -> print_endline "CLOSING BYE"; push None
| hello -> print_endline ("> " ^ hello))
stream
in print_endline server
|