wip: state server side; websocket
This commit is contained in:
parent
549aa39e09
commit
91cff202f6
11 changed files with 169 additions and 44 deletions
27
src/ws.ml
Normal file
27
src/ws.ml
Normal 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 ()
|
||||
Loading…
Add table
Add a link
Reference in a new issue