| Crates.io | mr_socketio |
| lib.rs | mr_socketio |
| version | 0.1.1 |
| created_at | 2025-01-20 04:36:32.586833+00 |
| updated_at | 2025-01-20 08:25:07.998515+00 |
| description | A brief description of what your crate does |
| homepage | |
| repository | https://github.com/supaatwi/mr_socketio |
| max_upload_size | |
| id | 1523561 |
| size | 65,505 |
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(())
}
MIT