Fix the echo server example to prevent ConnectionClosed error (#189)

Related #188

The echo server would generate ConnectionClosed error when the peer
close the connection normally. This is because that `tungstenite` crate
automatically reply Close message to the peer. Then StreamExt::forward()
also forwards the Close message after the connection is closed.
This commit is contained in:
Leo 2021-09-16 23:19:27 +08:00 committed by Sebastian Dröge
parent 3af5ff5c3b
commit dfa69eaea4
1 changed files with 4 additions and 2 deletions

View File

@ -46,9 +46,11 @@ async fn accept_connection(stream: TcpStream) {
info!("New WebSocket connection: {}", addr);
let (write, read) = ws_stream.split();
read.forward(write)
// We should not forward messages other than text or binary.
read.try_filter(|msg| future::ready(msg.is_text() || msg.is_binary()))
.forward(write)
.await
.expect("Failed to forward message")
.expect("Failed to forward messages")
}
fn main() -> Result<(), Error> {