use crate::message::Message;
use async_trait::async_trait;
#[async_trait]
pub trait WebSocketHandler: Send + Sync + 'static {
async fn on_connect(&self, connection_id: &str) {
let _ = connection_id;
}
async fn on_message(&self, connection_id: &str, message: Message);
async fn on_disconnect(&self, connection_id: &str) {
let _ = connection_id;
}
async fn on_error(&self, connection_id: &str, error: &crate::error::WebSocketError) {
tracing::error!(connection_id = %connection_id, error = %error, "WebSocket error");
}
async fn on_ping(&self, connection_id: &str, payload: &[u8]) -> Vec<u8> {
let _ = connection_id;
payload.to_vec()
}
async fn on_pong(&self, connection_id: &str, payload: &[u8]) {
let _ = (connection_id, payload);
}
}
#[derive(Debug, Default, Clone)]
pub struct LoggingHandler;
#[async_trait]
impl WebSocketHandler for LoggingHandler {
async fn on_connect(&self, connection_id: &str) {
tracing::info!(connection_id = %connection_id, "Client connected");
}
async fn on_message(&self, connection_id: &str, message: Message) {
tracing::debug!(
connection_id = %connection_id,
message_type = ?message.message_type,
payload_len = message.payload.len(),
"Received message"
);
}
async fn on_disconnect(&self, connection_id: &str) {
tracing::info!(connection_id = %connection_id, "Client disconnected");
}
}