X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=warp-server%2Fsrc%2Fmain.rs;fp=warp-server%2Fsrc%2Fmain.rs;h=f4983e73ed45895573a7ca16dbf11459a4ac0348;hp=a89c7a6fd205e4703d04ed7f38a9232799dbd69a;hb=05975f1d5fcc331c82f3b9786a5f4afbc027d4cd;hpb=b2728f732e28cdd993c35870bf8cd5369f43a801 diff --git a/warp-server/src/main.rs b/warp-server/src/main.rs index a89c7a6..f4983e7 100644 --- a/warp-server/src/main.rs +++ b/warp-server/src/main.rs @@ -1,3 +1,4 @@ +use futures_util::{FutureExt, StreamExt}; use warp::Filter; #[tokio::main] @@ -7,7 +8,22 @@ async fn main() { .and(warp::header::("user-agent")) .map(|name, agent| format!("Hello, {} from {}!", name, agent)); - warp::serve(hello) + // websocat ws://127.0.0.1:3030/ws-echo + let echo = warp::path("ws-echo") + .and(warp::ws()) + .map(|ws: warp::ws::Ws| { + ws.on_upgrade(|websocket| { + // echo all messages back + let (tx, rx) = websocket.split(); + rx.forward(tx).map(|result| { + if let Err(e) = result { + eprintln!("websocket error: {:?}", e); + } + }) + }) + }); + + warp::serve(hello.or(echo)) .run(([127, 0, 0, 1], 3030)) .await; }