#[on]Expand description
Defines Socket.IO handlers for its associated adapter.
This macro should be used inside an impl block of a struct annotated with the #[socketio_adapter] macro.
§Parameters
path: The path for the Socket.IO endpoint, e.g.,"/socket"
§Usage
ⓘ
#[socketio_adapter("/socket")]
struct SocketController;
#[handlers]
impl SocketController {
#[on_connection]
async fn on_connect(&self, socket: SocketRef) {
println!("Client connected");
}
#[on_message("message")]
async fn on_message(&self, socket: SocketRef, Data(msg): Data<String>) {
println!("Received: {}", msg);
}
#[on_disconnect]
async fn on_disconnect(&self, socket: SocketRef) {
println!("Client disconnected");
}
}Unified handler attribute for Socket.IO events.
§Event Types
#[on("connection")]- Called when a client connects#[on("disconnection")]- Called when a client disconnects#[on("fallback")]- Called for unhandled events#[on("custom_event")]- Called for custom event names
§Parameters
All handlers receive &self and ctx: SocketContext which provides access to:
- Socket operations via
ctx.socket - Message data via
ctx.try_data::<T>() - Event name via
ctx.event() - Acknowledgments via
ctx.ack()
§Usage
ⓘ
#[socketio_adapter("/chat")]
pub struct ChatAdapter { ... }
impl ChatAdapter {
#[on("connection")]
async fn on_connect(&self, ctx: SocketContext) {
println!("Client connected: {}", ctx.socket.id);
}
#[on("message")]
async fn handle_message(&self, ctx: SocketContext) {
let msg: String = ctx.try_data().unwrap();
println!("Received: {}", msg);
}
}