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 *) Dream.log " SEND USER ISLAND"; (* 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 Dream.log " SENDED USER ISLAND"; 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 ()