2 releases

0.1.1 Jan 20, 2025
0.1.0 Jan 20, 2025

#5 in #socket-client

Download history 197/week @ 2025-01-20

197 downloads per month

MIT/Apache

18KB
204 lines

Mr socketio

Example usage (Client)

Add the following to your Cargo.toml file:

mr_socketio = "*"

Then you're able to run the following example code:

use std::sync::Arc;
use mr_socketio::client::SocketIOClient;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
 
let client = Arc::new(SocketIOClient::new("host"));

    println!("Starting connection...");
    client.on("chat", |data| {
        println!("Received message event: {:?}", data);
    }).await;

    // Connect in a separate task
    let connect_handle = {
        let client = client.clone();
        tokio::spawn(async move {
            client.connect().await
        })
    };


    println!("Wait a bit for the connection to establish");
    loop {
        tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
        if client.is_connected().await {
            break;
        }
    }

    // Emit the event
    println!("Emitting chat event...");
    client.emit("chat", json!({
        "say": "Hello Socket Client"
    })).await?;


    // Wait for the connection task to complete
    connect_handle.await??;

    Ok(())
}

Licence

MIT

Dependencies

~9–23MB
~339K SLoC