ClientRunner

Struct ClientRunner 

Source
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 the AppState trait.

§Methods

§new

Constructs a new ClientRunner instance.

§Arguments

  • connector: An Arc to a type implementing the Connector trait, responsible for establishing connections.
  • state: An Arc to the application state.
  • router: An Arc to the message Router.
  • 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_connect or on_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>

Source

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:

  1. Connection Establishment (on_connect): Called after successful connection
  2. Message Sending (on_send): Called before each message is sent to WebSocket
  3. Message Receiving (on_receive): Called for each incoming message (in Router::route)
  4. Disconnection (on_disconnect): Called on manual disconnect, shutdown, or connection loss
§Connection Lifecycle
  • Connection: Middleware on_connect is called after successful WebSocket connection
  • Active Session: Middleware on_send/on_receive called for each message
  • Disconnection: Middleware on_disconnect called 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more