do not send whole state on action
This commit is contained in:
parent
86489c5394
commit
caffcbb527
6 changed files with 135 additions and 66 deletions
34
src/ws.ml
34
src/ws.ml
|
|
@ -6,23 +6,37 @@ let handle_client request client =
|
|||
| 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 state_msg = Network.Full_state state in
|
||||
|
||||
let rec loop () =
|
||||
(* send user island state *)
|
||||
let* () =
|
||||
Dream.send ~text_or_binary:`Text client (Network.marshal state_msg)
|
||||
in
|
||||
|
||||
let rec loop state =
|
||||
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 ()
|
||||
let (Network.Action_msg action : Network.client_message) =
|
||||
Network.unmarshal s
|
||||
in
|
||||
let res, state =
|
||||
match State.check_action state action with
|
||||
| Error _e as error -> (error, state)
|
||||
| Ok action' ->
|
||||
(* update server state *)
|
||||
let state = State.perform_action state action' in
|
||||
User.set_state user_id state;
|
||||
(Ok action', state)
|
||||
in
|
||||
let* () =
|
||||
Dream.send client (Network.marshal (Network.Update_result res))
|
||||
in
|
||||
loop state
|
||||
in
|
||||
loop ()
|
||||
loop state
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue