-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
viz_echo.rs
47 lines (36 loc) · 1.26 KB
/
viz_echo.rs
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
use rmpv::Value;
use socketioxide::{
extract::{AckSender, Data, SocketRef},
SocketIo,
};
use tracing::{error, info};
use tracing_subscriber::FmtSubscriber;
use viz::{handler::ServiceHandler, serve, Result, Router};
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", |socket: SocketRef, Data::<Value>(data)| {
info!(?data, "Received event");
socket.emit("message-back", &data).ok();
});
socket.on("message-with-ack", |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 (svc, io) = SocketIo::new_svc();
io.ns("/", on_connect);
io.ns("/custom", on_connect);
let app = Router::new()
.get("/", |_| async { Ok("Hello, World!") })
.any("/*", ServiceHandler::new(svc));
info!("Starting server");
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
if let Err(e) = serve(listener, app).await {
error!("{}", e);
}
Ok(())
}