pub trait WebSocketHandler: Send + Sync {
// Required methods
fn on_message(
&self,
message: Message,
) -> impl Future<Output = WebSocketResult<()>> + Send;
fn on_connect(&self) -> impl Future<Output = WebSocketResult<()>> + Send;
fn on_disconnect(&self) -> impl Future<Output = WebSocketResult<()>> + Send;
// Provided methods
fn reconnection_config(&self) -> Option<ReconnectionConfig> { ... }
fn on_reconnecting(
&self,
_attempt: u32,
) -> impl Future<Output = WebSocketResult<()>> + Send { ... }
fn on_reconnected(&self) -> impl Future<Output = WebSocketResult<()>> + Send { ... }
fn on_reconnect_failed(
&self,
_total_attempts: u32,
) -> impl Future<Output = WebSocketResult<()>> + Send { ... }
}Expand description
WebSocket handler trait
Required Methods§
Sourcefn on_message(
&self,
message: Message,
) -> impl Future<Output = WebSocketResult<()>> + Send
fn on_message( &self, message: Message, ) -> impl Future<Output = WebSocketResult<()>> + Send
Handle incoming message
Sourcefn on_connect(&self) -> impl Future<Output = WebSocketResult<()>> + Send
fn on_connect(&self) -> impl Future<Output = WebSocketResult<()>> + Send
Handle connection open
Sourcefn on_disconnect(&self) -> impl Future<Output = WebSocketResult<()>> + Send
fn on_disconnect(&self) -> impl Future<Output = WebSocketResult<()>> + Send
Handle connection close
Provided Methods§
Sourcefn reconnection_config(&self) -> Option<ReconnectionConfig>
fn reconnection_config(&self) -> Option<ReconnectionConfig>
Returns the reconnection configuration for this handler.
Override this method to enable automatic reconnection with custom settings.
Returns None by default, meaning no automatic reconnection is performed.
§Examples
use reinhardt_websockets::handler::WebSocketHandler;
use reinhardt_websockets::{Message, WebSocketResult};
use reinhardt_websockets::reconnection::ReconnectionConfig;
use std::time::Duration;
struct MyHandler;
impl WebSocketHandler for MyHandler {
fn reconnection_config(&self) -> Option<ReconnectionConfig> {
Some(ReconnectionConfig::default()
.with_max_attempts(5)
.with_initial_delay(Duration::from_secs(1)))
}
}Sourcefn on_reconnecting(
&self,
_attempt: u32,
) -> impl Future<Output = WebSocketResult<()>> + Send
fn on_reconnecting( &self, _attempt: u32, ) -> impl Future<Output = WebSocketResult<()>> + Send
Called when a reconnection attempt is about to be made.
This hook allows handlers to perform any setup needed before attempting to reconnect (e.g., refreshing tokens, logging).
The attempt parameter indicates which attempt this is (1-based).
Returns Ok(()) to proceed with reconnection, or Err to abort.
Sourcefn on_reconnected(&self) -> impl Future<Output = WebSocketResult<()>> + Send
fn on_reconnected(&self) -> impl Future<Output = WebSocketResult<()>> + Send
Called when a reconnection attempt succeeds.
This hook allows handlers to perform post-reconnection setup (e.g., resubscribing to channels, restoring state).
Sourcefn on_reconnect_failed(
&self,
_total_attempts: u32,
) -> impl Future<Output = WebSocketResult<()>> + Send
fn on_reconnect_failed( &self, _total_attempts: u32, ) -> impl Future<Output = WebSocketResult<()>> + Send
Called when all reconnection attempts have been exhausted.
This hook allows handlers to perform cleanup when reconnection is no longer possible (e.g., notifying the user, releasing resources).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.