pub struct WsManager { /* private fields */ }Expand description
Manages a single WebSocket feed: connect, receive, reconnect.
Call WsManager::run to enter the connection loop. Messages are forwarded
to the mpsc::Sender supplied to run; the loop retries on disconnection
according to the ReconnectPolicy in the ConnectionConfig.
Implementations§
Source§impl WsManager
impl WsManager
Sourcepub fn new(config: ConnectionConfig) -> Self
pub fn new(config: ConnectionConfig) -> Self
Create a new manager from a validated ConnectionConfig.
Sourcepub async fn run(
&mut self,
message_tx: Sender<String>,
outbound_rx: Option<Receiver<String>>,
) -> Result<(), StreamError>
pub async fn run( &mut self, message_tx: Sender<String>, outbound_rx: Option<Receiver<String>>, ) -> Result<(), StreamError>
Run the WebSocket connection loop, forwarding text messages to message_tx.
The loop connects, reads frames until the socket closes or errors, then waits the configured backoff and reconnects. Returns when either:
message_txis closed (receiver dropped), or- reconnect attempts are exhausted (
StreamError::ReconnectExhausted).
outbound_rx is an optional channel for sending messages to the
server (e.g., subscription requests). When provided, any string received
on this channel is forwarded to the WebSocket as a text frame.
§Errors
Returns StreamError::ReconnectExhausted after all reconnect slots
are consumed, or the underlying connection error if reconnects are
exhausted immediately on the first attempt.
Sourcepub fn connect_simulated(&mut self)
pub fn connect_simulated(&mut self)
Simulate a connection (for testing without live WebSocket).
Increments connect_attempts to reflect the initial connection slot.
Sourcepub fn disconnect_simulated(&mut self)
pub fn disconnect_simulated(&mut self)
Simulate a disconnection.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Whether the managed connection is currently in the connected state.
Sourcepub fn connect_attempts(&self) -> u32
pub fn connect_attempts(&self) -> u32
Total connection attempts made so far (including the initial connect).
Sourcepub fn config(&self) -> &ConnectionConfig
pub fn config(&self) -> &ConnectionConfig
The configuration this manager was created with.
Sourcepub fn can_reconnect(&self) -> bool
pub fn can_reconnect(&self) -> bool
Check whether the next reconnect attempt is allowed.
Sourcepub fn next_reconnect_backoff(&mut self) -> Result<Duration, StreamError>
pub fn next_reconnect_backoff(&mut self) -> Result<Duration, StreamError>
Consume a reconnect slot and return the backoff duration to wait.