wip: state server side; websocket

This commit is contained in:
Swrup 2022-12-11 18:58:56 +01:00
parent 3c6a373dc9
commit 5f1d29bda3
11 changed files with 169 additions and 44 deletions

27
src/ws.ml Normal file
View file

@ -0,0 +1,27 @@
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 *)
(* 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
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 ()