rustwave 0.1.0

A lightweight async event-driven WebSocket framework for Rust
Documentation
use rustwave::{Client, Result};
use serde::Serialize;
use tokio::time::{Duration, sleep};

#[derive(Serialize)]
struct GameMessage {
    mess: String,
    room: String,
}

const ROOM: &str = "room-1";

#[tokio::main]
async fn main() -> Result<()> {
    let client = Client::connect("ws://127.0.0.1:8080/game").await?;

    client.on("online-count", |data| async move {
        println!("Online in /game: {}", data);
    });

    client.on("player-joined", |data| async move {
        println!("Someone joined the room: {}", data);
    });

    client.on("player-left", |data| async move {
        println!("Someone left the room: {}", data);
    });

    client.on("typing", |data| async move {
        println!("{}", data);
    });

    client.on("mess", |data| async move {
        println!("Room message: {}", data);
    });

    client.emit("online-count", "").await?;
    sleep(Duration::from_millis(300)).await;

    client.emit("join-room", ROOM).await?;
    sleep(Duration::from_millis(300)).await;

    client.emit("typing", ROOM).await?;
    sleep(Duration::from_millis(300)).await;

    let msg = GameMessage {
        mess: "hey room, I'm here!".to_string(),
        room: ROOM.to_string(),
    };
    client.emit("mess", msg).await?;
    sleep(Duration::from_millis(300)).await;

    client.emit("leave-room", ROOM).await?;

    sleep(Duration::from_secs(3)).await;

    Ok(())
}