tidy up
This commit is contained in:
parent
43babbbfab
commit
eee34eb3ff
2 changed files with 17 additions and 16 deletions
|
@ -5,7 +5,7 @@ use anyhow::{Context, Result};
|
||||||
use future::ready;
|
use future::ready;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use net::{
|
use net::{
|
||||||
server::{Handle, Server, run_client},
|
server::{run_client, Handle, Server},
|
||||||
ClientMessage, ServerMessage,
|
ClientMessage, ServerMessage,
|
||||||
};
|
};
|
||||||
use serde_json::{from_str, to_string};
|
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() {
|
ready(match msg.to_str() {
|
||||||
Ok(json) => from_str::<ClientMessage>(json)
|
Ok(json) => from_str::<ClientMessage>(json)
|
||||||
.context("Parsing JSON")
|
.context("Parsing JSON")
|
||||||
.map(Some),
|
.map(Some),
|
||||||
Err(()) => {
|
Err(()) => {
|
||||||
debug!("Non-text message");
|
debug!("Non-text message {:?}", &msg);
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -101,16 +101,15 @@ pub async fn run_client(
|
||||||
|
|
||||||
let input_task = async {
|
let input_task = async {
|
||||||
loop {
|
loop {
|
||||||
match source.next().await {
|
match source.try_next().await? {
|
||||||
Some(Ok(msg)) => handle.server.lock().await.process_message(player_id, msg),
|
Some(msg) => handle.server.lock().await.process_message(player_id, msg),
|
||||||
Some(Err(error)) => return Err(error),
|
|
||||||
None => break Ok(()),
|
None => break Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let result = try_join(output_task, input_task).await.map(|_| ());
|
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);
|
handle.server.lock().await.remove_player(player_id);
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
Loading…
Reference in a new issue