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<()> {
|
async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()> {
|
||||||
let (sink, source) = websocket.split();
|
let mut websocket = websocket.with(|msg: ServerMessage| {
|
||||||
|
|
||||||
let mut sink = sink.with(|msg: ServerMessage| {
|
|
||||||
ready(
|
ready(
|
||||||
to_string(&msg)
|
to_string(&msg)
|
||||||
.context("JSON encoding shouldn't fail")
|
.context("JSON encoding shouldn't fail")
|
||||||
.map(|json| Message::text(json)),
|
.map(|json| Message::text(json)),
|
||||||
)
|
)
|
||||||
});
|
}).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")
|
||||||
|
@ -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(
|
pub async fn run_client(
|
||||||
handle: Handle,
|
handle: Handle,
|
||||||
source: &mut (impl Stream<Item = Result<ClientMessage, Error>> + Send + Unpin),
|
socket: &mut (impl Stream<Item = Result<ClientMessage, Error>> + Sink<ServerMessage, Error = Error> + Send + Unpin),
|
||||||
sink: &mut (impl Sink<ServerMessage, Error = Error> + Send + Unpin),
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
let (sink, mut source) = socket.split();
|
||||||
let (sender, receiver) = channel(CHANNEL_BUFFER);
|
let (sender, receiver) = channel(CHANNEL_BUFFER);
|
||||||
|
|
||||||
// register player
|
// register player
|
||||||
|
|
Loading…
Reference in a new issue