Source

amall / tests / test_websocket_service.ml

Full commit
open Am_All
;
open Amall_types
;

module IO = IO_Lwt;

module I = Iteratees.Make(IO);

(*
module II : sig exception EIO of (exn * It_Types.place); end = I;
*)

module S = Amall_http_service.Service(IO)(I);
module Ws = S.H.Ws;


value () = dbg "point1"
;

value (my_listener, _http_root, ws_root) = S.listener_create (`Inet_any 12345)
;

value () = dbg "point2"
;

value my_endpoint = (ws_root, `Service ([""; "localhost:12345"], "wstest"))
;

value () = dbg "point3"
;

open Amall_http;

value () = dbg "point4"
;

open Printf;

open Cd_All;

open I.Ops;

value my_func = Partapp3.make
  (fun segpath _rq outws ->
     let txt = sprintf "[%s]" &
       String.concat " ; " &
       List.map (sprintf "%S") &
       segpath
     in
     let () = dbg "service: got websocket request on %s" txt in
     fun opcode_in ->
       match opcode_in with
       [ `Text ->
           I.gather_to_string >>= fun s ->
           let () = dbg "service: got text frame: %S" s in
           I.return ()
       | `Close opt_code ->
           let () = dbg "service: got `Close (%s)"
             (match opt_code with
              [ None -> "None"
              | Some c -> sprintf "Some %i" c
              ]
             )
           in
           Ws.it_close outws opt_code
       | _ ->
           I.gather_to_string >>= fun s ->
           let () = dbg "service: got non-text non-close frame: %S" s in
           I.return ()
       ]
  )
;

value () = dbg "point5"
;

value () = (dbg "pre"; S.mount_websocket my_endpoint my_func; dbg "post")
;

value () = S.listener_run my_listener
;