Skip to main content

reinhardt_websockets/
handler.rs

1//! WebSocket handler
2
3use crate::connection::Message;
4use crate::connection::WebSocketResult;
5use crate::reconnection::ReconnectionConfig;
6
7/// WebSocket handler trait
8pub trait WebSocketHandler: Send + Sync {
9	/// Handle incoming message
10	fn on_message(
11		&self,
12		message: Message,
13	) -> impl std::future::Future<Output = WebSocketResult<()>> + Send;
14
15	/// Handle connection open
16	fn on_connect(&self) -> impl std::future::Future<Output = WebSocketResult<()>> + Send;
17
18	/// Handle connection close
19	fn on_disconnect(&self) -> impl std::future::Future<Output = WebSocketResult<()>> + Send;
20
21	/// Returns the reconnection configuration for this handler.
22	///
23	/// Override this method to enable automatic reconnection with custom settings.
24	/// Returns `None` by default, meaning no automatic reconnection is performed.
25	///
26	/// # Examples
27	///
28	/// ```rust,no_run
29	/// use reinhardt_websockets::handler::WebSocketHandler;
30	/// use reinhardt_websockets::{Message, WebSocketResult};
31	/// use reinhardt_websockets::reconnection::ReconnectionConfig;
32	/// use std::time::Duration;
33	///
34	/// struct MyHandler;
35	///
36	/// impl WebSocketHandler for MyHandler {
37	///     # async fn on_message(&self, _: Message) -> WebSocketResult<()> { Ok(()) }
38	///     # async fn on_connect(&self) -> WebSocketResult<()> { Ok(()) }
39	///     # async fn on_disconnect(&self) -> WebSocketResult<()> { Ok(()) }
40	///     fn reconnection_config(&self) -> Option<ReconnectionConfig> {
41	///         Some(ReconnectionConfig::default()
42	///             .with_max_attempts(5)
43	///             .with_initial_delay(Duration::from_secs(1)))
44	///     }
45	/// }
46	/// ```
47	fn reconnection_config(&self) -> Option<ReconnectionConfig> {
48		None
49	}
50
51	/// Called when a reconnection attempt is about to be made.
52	///
53	/// This hook allows handlers to perform any setup needed before
54	/// attempting to reconnect (e.g., refreshing tokens, logging).
55	///
56	/// The `attempt` parameter indicates which attempt this is (1-based).
57	///
58	/// Returns `Ok(())` to proceed with reconnection, or `Err` to abort.
59	fn on_reconnecting(
60		&self,
61		_attempt: u32,
62	) -> impl std::future::Future<Output = WebSocketResult<()>> + Send {
63		async { Ok(()) }
64	}
65
66	/// Called when a reconnection attempt succeeds.
67	///
68	/// This hook allows handlers to perform post-reconnection setup
69	/// (e.g., resubscribing to channels, restoring state).
70	fn on_reconnected(&self) -> impl std::future::Future<Output = WebSocketResult<()>> + Send {
71		async { Ok(()) }
72	}
73
74	/// Called when all reconnection attempts have been exhausted.
75	///
76	/// This hook allows handlers to perform cleanup when reconnection
77	/// is no longer possible (e.g., notifying the user, releasing resources).
78	fn on_reconnect_failed(
79		&self,
80		_total_attempts: u32,
81	) -> impl std::future::Future<Output = WebSocketResult<()>> + Send {
82		async { Ok(()) }
83	}
84}