-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathwarp_echo.rs
More file actions
64 lines (54 loc) · 2.03 KB
/
warp_echo.rs
File metadata and controls
64 lines (54 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use hyper::server::conn::http1;
use hyper_util::{rt::TokioIo, service::TowerToHyperService};
use rmpv::Value;
use socketioxide::{
extract::{AckSender, Data, SocketRef},
SocketIo,
};
use tracing::info;
use tracing_subscriber::FmtSubscriber;
use warp::Filter;
async fn on_connect(socket: SocketRef, Data(data): Data<Value>) {
info!(ns = socket.ns(), ?socket.id, "Socket.IO connected");
socket.emit("auth", &data).ok();
socket.on("message", async |socket: SocketRef, Data::<Value>(data)| {
info!(?data, "Received event:");
socket.emit("message-back", &data).ok();
});
socket.on(
"message-with-ack",
async |Data::<Value>(data), ack: AckSender| {
info!(?data, "Received event");
ack.send(&data).ok();
},
);
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing::subscriber::set_global_default(FmtSubscriber::default())?;
let warp_svc = warp::service(warp::any().map(|| "Hello, World!".to_string()));
let (svc, io) = SocketIo::new_inner_svc(warp_svc);
io.ns("/", on_connect);
io.ns("/custom", on_connect);
info!("Starting server");
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
// We start a loop to continuously accept incoming connections
loop {
let (stream, _) = listener.accept().await?;
// Use an adapter to access something implementing `tokio::io` traits as if they implement
// `hyper::rt` IO traits.
let io = TokioIo::new(stream);
let svc = TowerToHyperService::new(svc.clone());
// Spawn a tokio task to serve multiple connections concurrently
tokio::task::spawn(async move {
// Finally, we bind the incoming connection to our `hello` service
if let Err(err) = http1::Builder::new()
.serve_connection(io, svc)
.with_upgrades()
.await
{
println!("Error serving connection: {:?}", err);
}
});
}
}