Controladores Socket.IO
En Sword, un controlador Socket.IO es un struct anotado con #[controller(kind = Controller::SocketIo, namespace = "...")] cuyos métodos manejan eventos declarados con #[on("...")].
Definir un controlador
rust
use sword::prelude::*;
use sword::socketio::*;
#[controller(kind = Controller::SocketIo, namespace = "/chat")]
pub struct ChatController;
impl ChatController {
#[on("connection")]
async fn on_connect(&self, ctx: SocketContext) {
println!("Client connected: {}", ctx.id());
}
#[on("message")]
async fn on_message(&self, ctx: SocketContext) {
let Ok(message) = ctx.try_data::<String>() else {
return;
};
ctx.socket.emit("message", &message).ok();
}
}Eventos soportados
Eventos especiales
#[on("connection")]#[on("disconnection")]#[on("fallback")]
Eventos personalizados
#[on("message")]#[on("chat-message")]#[on("room:join")]
Registro en un módulo
rust
use sword::prelude::*;
pub struct ChatModule;
impl Module for ChatModule {
fn register_controllers(controllers: &ControllerRegistry) {
controllers.register::<ChatController>();
}
}
