Adapt the socket in main without having to split it
This commit is contained in:
parent
7a793cf2c6
commit
82f1f81760
2 changed files with 5 additions and 9 deletions
10
src/main.rs
10
src/main.rs
|
@ -94,17 +94,13 @@ fn serve_file(path: &str) -> Result<impl Reply, Rejection> {
|
|||
}
|
||||
|
||||
async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()> {
|
||||
let (sink, source) = websocket.split();
|
||||
|
||||
let mut sink = sink.with(|msg: ServerMessage| {
|
||||
let mut websocket = websocket.with(|msg: ServerMessage| {
|
||||
ready(
|
||||
to_string(&msg)
|
||||
.context("JSON encoding shouldn't fail")
|
||||
.map(|json| Message::text(json)),
|
||||
)
|
||||
});
|
||||
|
||||
let mut source = source.map_err(Into::into).try_filter_map(|msg| {
|
||||
}).map_err(Into::into).try_filter_map(|msg| {
|
||||
ready(match msg.to_str() {
|
||||
Ok(json) => from_str::<ClientMessage>(json)
|
||||
.context("Parsing JSON")
|
||||
|
@ -116,5 +112,5 @@ async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()>
|
|||
})
|
||||
});
|
||||
|
||||
run_client(game_server, &mut source, &mut sink).await
|
||||
run_client(game_server, &mut websocket).await
|
||||
}
|
||||
|
|
|
@ -204,9 +204,9 @@ impl Server {
|
|||
|
||||
pub async fn run_client(
|
||||
handle: Handle,
|
||||
source: &mut (impl Stream<Item = Result<ClientMessage, Error>> + Send + Unpin),
|
||||
sink: &mut (impl Sink<ServerMessage, Error = Error> + Send + Unpin),
|
||||
socket: &mut (impl Stream<Item = Result<ClientMessage, Error>> + Sink<ServerMessage, Error = Error> + Send + Unpin),
|
||||
) -> Result<()> {
|
||||
let (sink, mut source) = socket.split();
|
||||
let (sender, receiver) = channel(CHANNEL_BUFFER);
|
||||
|
||||
// register player
|
||||
|
|
Loading…
Reference in a new issue