Browse Source

move scanf to Msg

trunk
Swrup 10 months ago
parent
commit
cc9618b979
  1. 7
      src/dromloc.ml
  2. 6
      src/js/client.ml
  3. 20
      src/msg.ml

7
src/dromloc.ml

@ -68,12 +68,9 @@ let handle_client request client =
forget_client room id;
Dream.close_websocket client
| Some data -> (
let s = Scanf.sscanf data "%S" (fun s -> s) in
let message_to_broadcast_res =
Result.bind (Msg.decode_pos s) (fun pos ->
Result.map
(fun s -> Format.sprintf "%S" s)
(Msg.encode_pos_with_id (id, pos)) )
Result.bind (Msg.decode_pos data) (fun pos ->
Msg.encode_pos_with_id (id, pos) )
in
match message_to_broadcast_res with
| Error e ->

6
src/js/client.ml

@ -42,8 +42,7 @@ let () =
match Msg.encode_pos (jv_to_pos jv_geo) with
| Error e -> failwith e
| Ok s ->
let s = Format.sprintf "%S" s |> Jstr.of_string in
Websocket.send_string ws s;
Websocket.send_string ws (Jstr.of_string s);
log "msg was send to websocket ~~ @\n"
in
let _watch_id = Geo.watch_geolocation send in
@ -93,8 +92,7 @@ let () =
log " on_message@\n";
let id, pos =
let data = Message.Ev.data (Ev.as_type m) |> Jstr.to_string in
let s = Scanf.sscanf data "%S" (fun s -> s) in
match Msg.decode_pos_with_id s with Error e -> failwith e | Ok v -> v
match Msg.decode_pos_with_id data with Error e -> failwith e | Ok v -> v
in
match Camel.find_opt id with
| None -> Camel.add id pos

20
src/msg.ml

@ -28,23 +28,27 @@ let pos_encoding = conv pos_to_tuple tuple_to_pos (tup4 float float float float)
let pos_with_id_encoding = tup2 string pos_encoding
let encode_pos v =
Result.map_error
(fun e -> Format.asprintf "Failed to encode pos: %a" Binary.pp_write_error e)
(Binary.to_string pos_encoding v)
match Binary.to_string pos_encoding v with
| Error e ->
Error (Format.asprintf "Failed to encode pos: %a" Binary.pp_write_error e)
| Ok s -> Ok (Format.sprintf "%S" s)
let encode_pos_with_id v =
Result.map_error
(fun e ->
Format.asprintf "Failed to encode pos with id: %a" Binary.pp_write_error e
)
(Binary.to_string pos_with_id_encoding v)
match Binary.to_string pos_with_id_encoding v with
| Error e ->
Error
(Format.asprintf "Failed to encode pos with id: %a" Binary.pp_write_error
e )
| Ok s -> Ok (Format.sprintf "%S" s)
let decode_pos v =
let v = Scanf.sscanf v "%S" (fun s -> s) in
Result.map_error
(fun e -> Format.asprintf "Failed to decode pos: %a" Binary.pp_read_error e)
(Binary.of_string pos_encoding v)
let decode_pos_with_id v =
let v = Scanf.sscanf v "%S" (fun s -> s) in
Result.map_error
(fun e ->
Format.asprintf "Failed to decode pos with id: %a" Binary.pp_read_error e

Loading…
Cancel
Save