Do not panic on normal errors

This commit is contained in:
Greg Heartsfield 2021-11-20 21:33:35 -06:00
parent f9e30c194f
commit 097199ae6a

View File

@ -1,17 +1,7 @@
//! A simple echo server.
//!
//! You can test this out by running:
//!
//! cargo run --example server 127.0.0.1:12345
//!
//! And then in another window run:
//!
//! cargo run --example client ws://127.0.0.1:12345/
use std::{env, io::Error}; use std::{env, io::Error};
use futures_util::{future, StreamExt, TryStreamExt}; use futures_util::{future, StreamExt, TryStreamExt};
use log::info; use log::{info, warn};
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
#[tokio::main] #[tokio::main]
@ -39,10 +29,10 @@ async fn accept_connection(stream: TcpStream) {
.expect("connected streams should have a peer address"); .expect("connected streams should have a peer address");
info!("Peer address: {}", addr); info!("Peer address: {}", addr);
let ws_stream = tokio_tungstenite::accept_async(stream) let ws_stream_res = tokio_tungstenite::accept_async(stream).await;
.await
.expect("Error during the websocket handshake occurred");
match ws_stream_res {
Ok(ws_stream) => {
info!("New WebSocket connection: {}", addr); info!("New WebSocket connection: {}", addr);
let (write, read) = ws_stream.split(); let (write, read) = ws_stream.split();
@ -50,5 +40,11 @@ async fn accept_connection(stream: TcpStream) {
read.try_filter(|msg| future::ready(msg.is_text() || msg.is_binary())) read.try_filter(|msg| future::ready(msg.is_text() || msg.is_binary()))
.forward(write) .forward(write)
.await .await
.expect("Failed to forward messages") .unwrap_or_else(|_| warn!("Failed to forward message"));
}
Err(_) => {
println!("Error");
info!("Error during websocket handshake");
}
};
} }