pellest/src/ws.ml

29 lines
882 B
OCaml
Raw Normal View History

2022-12-11 18:58:56 +01:00
open Lwt.Syntax
open Shared
let handle_client request client =
match Dream.session "user_id" request with
| None -> Dream.log "User does not exists" |> Lwt.return
| Some user_id ->
(* TODO catch marshal failure *)
2022-12-15 19:59:42 +01:00
Dream.log " SEND USER ISLAND";
2022-12-11 18:58:56 +01:00
(* send user island state *)
let state =
match User.get_state user_id with
| Error _e -> assert false
| Ok state -> state
in
let* () = Dream.send ~text_or_binary:`Text client (Network.marshal state) in
2022-12-15 19:59:42 +01:00
Dream.log " SENDED USER ISLAND";
2022-12-11 18:58:56 +01:00
let rec loop () =
match%lwt Dream.receive client with
| None -> Dream.close_websocket client
| Some s ->
let action : State.action = Network.unmarshal s in
let state_res = State.handle_action state action in
let* () = Dream.send client (Network.marshal state_res) in
loop ()
in
loop ()