pub struct ClientRunner<S: AppState> { /* private fields */ }Expand description
Implementation of the ClientRunner for managing WebSocket client connections and session lifecycle.
§Type Parameters
S: The application state type, which must implement theAppStatetrait.
§Methods
§new
Constructs a new ClientRunner instance.
§Arguments
connector: AnArcto a type implementing theConnectortrait, responsible for establishing connections.state: AnArcto the application state.router: AnArcto the messageRouter.to_ws_sender: An asynchronous sender for outgoing WebSocket messages.to_ws_receiver: An asynchronous receiver for outgoing WebSocket messages.runner_command_rx: An asynchronous receiver for runner commands (e.g., disconnect, shutdown).connection_callback: Callbacks to execute on connect and reconnect events.
§run
Asynchronously runs the main client loop, managing connection cycles, message routing, and command handling.
- Continuously attempts to connect or reconnect to the WebSocket server until a shutdown is requested.
- On successful connection, executes the appropriate connection callback (
on_connectoron_reconnect). - Spawns writer and reader tasks for handling outgoing and incoming WebSocket messages.
- Listens for runner commands (e.g., disconnect, shutdown) and manages session state accordingly.
- Handles unexpected connection loss and retries connection as needed.
- Cleans up resources and tasks on disconnect or shutdown.
§Behavior
- Uses a hard connect or reconnect based on the internal state.
- Retries connection attempts with a delay on failure.
- Ensures proper cleanup of tasks and state on disconnect or shutdown.
- Prints status messages for key events and errors.
Implementations§
Source§impl<S: AppState> ClientRunner<S>
impl<S: AppState> ClientRunner<S>
Sourcepub async fn run(&mut self)
pub async fn run(&mut self)
Main client runner loop that manages WebSocket connections and message processing.
§Middleware Integration Points
This method integrates middleware at four key points:
- Connection Establishment (
on_connect): Called after successful connection - Message Sending (
on_send): Called before each message is sent to WebSocket - Message Receiving (
on_receive): Called for each incoming message (in Router::route) - Disconnection (
on_disconnect): Called on manual disconnect, shutdown, or connection loss
§Connection Lifecycle
- Connection: Middleware
on_connectis called after successful WebSocket connection - Active Session: Middleware
on_send/on_receivecalled for each message - Disconnection: Middleware
on_disconnectcalled before cleanup
Examples found in repository?
examples/echo_client.rs (line 306)
288 pub async fn new(url: String) -> CoreResult<Self> {
289 // Use a simple connector (implement your own if needed)
290 let connector = DummyConnector::new(url);
291
292 let mut builder = ClientBuilder::new(connector, ());
293 builder =
294 builder.with_lightweight_handler(|msg, state, _| Box::pin(print_handler(msg, state)));
295 let (client, mut runner) = builder
296 .with_module::<EchoModule>()
297 .with_module::<StreamModule>()
298 .with_module::<PeriodicSenderModule>()
299 .build()
300 .await?;
301
302 // let echo_handle = client.get_handle::<EchoModule>().await.unwrap();
303 // let stream_handle = client.get_handle::<StreamModule>().await.unwrap();
304
305 // Start runner in background
306 let _runner = tokio::spawn(async move { runner.run().await });
307
308 Ok(Self { client, _runner })
309 }Auto Trait Implementations§
impl<S> Freeze for ClientRunner<S>
impl<S> !RefUnwindSafe for ClientRunner<S>
impl<S> Send for ClientRunner<S>
impl<S> Sync for ClientRunner<S>
impl<S> Unpin for ClientRunner<S>
impl<S> !UnwindSafe for ClientRunner<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more