This commit is contained in:
Tangent Wantwight 2020-05-30 19:16:50 -04:00
parent 43babbbfab
commit eee34eb3ff
2 changed files with 17 additions and 16 deletions

View file

@ -5,7 +5,7 @@ use anyhow::{Context, Result};
use future::ready;
use futures::prelude::*;
use net::{
server::{Handle, Server, run_client},
server::{run_client, Handle, Server},
ClientMessage, ServerMessage,
};
use serde_json::{from_str, to_string};
@ -72,13 +72,15 @@ async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()>
)
});
let mut source = source.map_err(Into::into).try_filter_map(|msg| {
let mut source = source
.map_err(Into::into)
.try_filter_map(|msg| {
ready(match msg.to_str() {
Ok(json) => from_str::<ClientMessage>(json)
.context("Parsing JSON")
.map(Some),
Err(()) => {
debug!("Non-text message");
debug!("Non-text message {:?}", &msg);
Ok(None)
}
})

View file

@ -101,16 +101,15 @@ pub async fn run_client(
let input_task = async {
loop {
match source.next().await {
Some(Ok(msg)) => handle.server.lock().await.process_message(player_id, msg),
Some(Err(error)) => return Err(error),
match source.try_next().await? {
Some(msg) => handle.server.lock().await.process_message(player_id, msg),
None => break Ok(()),
}
}
};
let result = try_join(output_task, input_task).await.map(|_| ());
// deregister player, whether normally or via error
// deregister player, whether normally or due to error
handle.server.lock().await.remove_player(player_id);
result