# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+futures-util = { version = "0.3", default-features = false, features = ["sink"] }
tokio = { version = "1", features = ["full"] }
warp = "0.3"
+use futures_util::{FutureExt, StreamExt};
use warp::Filter;
#[tokio::main]
.and(warp::header::<String>("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;
}