Skip to content

supaatwi/mr_socketio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages