add auto_state_update client & server
This commit is contained in:
parent
461d648ac9
commit
1850c5459d
4 changed files with 55 additions and 8 deletions
17
src/ws.ml
17
src/ws.ml
|
|
@ -18,25 +18,30 @@ let handle_client request client =
|
|||
Dream.send ~text_or_binary:`Text client (Network.marshal state_msg)
|
||||
in
|
||||
|
||||
let rec loop state =
|
||||
let rec loop () =
|
||||
match%lwt Dream.receive client with
|
||||
| None -> Dream.close_websocket client
|
||||
| Some s ->
|
||||
let state =
|
||||
match User.get_state user_id with
|
||||
| Error _e -> assert false
|
||||
| Ok state -> state
|
||||
in
|
||||
let (Network.Action_msg action : Network.client_message) =
|
||||
Network.unmarshal s
|
||||
in
|
||||
let res, state =
|
||||
let res =
|
||||
match State.check_action state action with
|
||||
| Error _e as error -> (error, state)
|
||||
| Error _e as error -> error
|
||||
| Ok action' ->
|
||||
(* update server state *)
|
||||
let state = State.perform_action state action' in
|
||||
User.set_state user_id state;
|
||||
(Ok action', state)
|
||||
Ok action'
|
||||
in
|
||||
let* () =
|
||||
Dream.send client (Network.marshal (Network.Update_result res))
|
||||
in
|
||||
loop state
|
||||
loop ()
|
||||
in
|
||||
loop state
|
||||
loop ()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue