Debian Codegraph @

third_party/rust/warp/examples/websockets.rs

raw
1 #![deny(warnings)]
2
3 use futures_util::{FutureExt, StreamExt};
4 use warp::Filter;
5
6 #[tokio::main]
7 async fn main() {
8 pretty_env_logger::init();
9
10 let routes = warp::path("echo")
11 // The `ws()` filter will prepare the Websocket handshake.
12 .and(warp::ws())
13 .map(|ws: warp::ws::Ws| {
14 // And then our closure will be called when it completes...
15 ws.on_upgrade(|websocket| {
16 // Just echo all messages back...
17 let (tx, rx) = websocket.split();
18 rx.forward(tx).map(|result| {
19 if let Err(e) = result {
20 eprintln!("websocket error: {:?}", e);
21 }
22 })
23 })
24 });
25
26 warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
27 }