aboutsummaryrefslogtreecommitdiff
path: root/test/js/websockets_hello.ml
blob: edcfb62f1172eb4c56336e827bdb7b1060ab3868 (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
36
37
38
39
40
41
42
43
44
45
46
47
open! Lwt.Syntax
open! Lwt.Infix
open! Js_of_ocaml
open! Flesh

(* 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 main (stream, push) config =
  let+ _auth = Sasl.authenticate (stream, push) config
  in match _auth with
     | Error (NotAuthorized, Some (_, text)) -> print_endline ("Not authorized: " ^ text)
     | Error (MalformedRequest, Some (_, text)) -> print_endline ("Malformed request: " ^ text)
     | Error _ -> print_endline "Error!"
     | Ok _ -> print_endline "Success!"

let () =
  run @@
    let config : Sasl.auth_config = {
        jid = (Sys.getenv "FLESH_JID");
        password = (Sys.getenv "FLESH_PASSWORD");
        preferred_mechanisms = [Stream.PLAIN]
      }
    in let domain = (List.nth (String.split_on_char '@' config.jid) 1) in
       let* stream, push = Stream.start domain in
       Lwt.catch
         (fun () -> main (stream, push) config >|= (fun () -> push None))
         (fun exn ->
           push None;
           (* I suspect JavaScript's [wrap_callback] swallows the Exceptions thrown by
              OCaml, so... The next best thing is probably printing something. *)
           print_endline
             (match exn with
              | Stream.InvalidStanza stanza -> "Invalid stanza: " ^ stanza
              | _ -> "... and so I stumble back to bed.");
           Lwt.fail exn)