1use rustwave::{Client, Result};
2use serde::Serialize;
3use tokio::time::{Duration, sleep};
4
5#[derive(Serialize)]
6struct GameMessage {
7 mess: String,
8 room: String,
9}
10
11const ROOM: &str = "room-1";
12
13#[tokio::main]
14async fn main() -> Result<()> {
15 let client = Client::connect("ws://127.0.0.1:8080/game").await?;
16
17 client.on("online-count", |data| async move {
18 println!("Online in /game: {}", data);
19 });
20
21 client.on("player-joined", |data| async move {
22 println!("Someone joined the room: {}", data);
23 });
24
25 client.on("player-left", |data| async move {
26 println!("Someone left the room: {}", data);
27 });
28
29 client.on("typing", |data| async move {
30 println!("{}", data);
31 });
32
33 client.on("mess", |data| async move {
34 println!("Room message: {}", data);
35 });
36
37 client.emit("online-count", "").await?;
38 sleep(Duration::from_millis(300)).await;
39
40 client.emit("join-room", ROOM).await?;
41 sleep(Duration::from_millis(300)).await;
42
43 client.emit("typing", ROOM).await?;
44 sleep(Duration::from_millis(300)).await;
45
46 let msg = GameMessage {
47 mess: "hey room, I'm here!".to_string(),
48 room: ROOM.to_string(),
49 };
50 client.emit("mess", msg).await?;
51 sleep(Duration::from_millis(300)).await;
52
53 client.emit("leave-room", ROOM).await?;
54
55 sleep(Duration::from_secs(3)).await;
56
57 Ok(())
58}