Skip to content

Commit

Permalink
Robust server example (#11)
Browse files Browse the repository at this point in the history
This handles connection errors. The server example does not exit
anymore, instead it reports them to stderr.

Closes #7
  • Loading branch information
boxdot authored Jun 4, 2024
1 parent 052ae1a commit da082ea
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions examples/helloworld/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let listener = TcpListener::bind(addr).await?;
let listener_stream = TcpListenerStream::new(listener);
let incoming = listener_stream.then(|connection| async {
let incoming = listener_stream.filter_map(|connection| async {
match connection {
Ok(tcp_stream) => {
let ws_stream = tokio_tungstenite::accept_async(tcp_stream).await.unwrap();
Ok(WsConnection::from_combined_channel(ws_stream))
let ws_stream = match tokio_tungstenite::accept_async(tcp_stream).await {
Ok(ws_stream) => ws_stream,
Err(e) => {
eprintln!("failed to accept connection: {e}");
return None;
}
};
Some(Ok(WsConnection::from_combined_channel(ws_stream)))
}
Err(e) => Err(e),
Err(e) => Some(Err(e)),
}
});

Expand Down

0 comments on commit da082ea

Please sign in to comment.