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:
parent
3af5ff5c3b
commit
dfa69eaea4
|
@ -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> {
|
||||
|
|
Loading…
Reference in New Issue