pub struct WsClient { /* private fields */ }Expand description
Async WebSocket client for exchange streaming APIs.
§Backpressure Handling
This client uses bounded channels to prevent memory exhaustion in high-frequency
trading scenarios. When the message channel is full, the configured
BackpressureStrategy determines how to handle new messages:
DropOldest: Removes the oldest message to make room (default)DropNewest: Discards the incoming messageBlock: Waits until space is available (may stall the read loop)
Implementations§
Source§impl WsClient
impl WsClient
Sourcepub fn new(config: WsConfig) -> WsClient
pub fn new(config: WsConfig) -> WsClient
Creates a new WebSocket client instance.
The client uses bounded channels for message passing to prevent memory
exhaustion. Channel capacities are configured via WsConfig.
Sourcepub async fn set_event_callback(
&self,
callback: Arc<dyn Fn(WsEvent) + Send + Sync>,
)
pub async fn set_event_callback( &self, callback: Arc<dyn Fn(WsEvent) + Send + Sync>, )
Sets the event callback for connection lifecycle events.
Sourcepub async fn clear_event_callback(&self)
pub async fn clear_event_callback(&self)
Clears the event callback.
Sourcepub async fn set_cancel_token(&self, token: CancellationToken)
pub async fn set_cancel_token(&self, token: CancellationToken)
Sets the cancellation token for this client.
Sourcepub async fn clear_cancel_token(&self)
pub async fn clear_cancel_token(&self)
Clears the cancellation token.
Sourcepub async fn get_cancel_token(&self) -> Option<CancellationToken>
pub async fn get_cancel_token(&self) -> Option<CancellationToken>
Returns a clone of the current cancellation token, if set.
Sourcepub async fn connect(&self) -> Result<(), Error>
pub async fn connect(&self) -> Result<(), Error>
Establishes connection to the WebSocket server.
Sourcepub async fn connect_with_cancel(
&self,
cancel_token: Option<CancellationToken>,
) -> Result<(), Error>
pub async fn connect_with_cancel( &self, cancel_token: Option<CancellationToken>, ) -> Result<(), Error>
Establishes connection with cancellation support.
Sourcepub async fn disconnect(&self) -> Result<(), Error>
pub async fn disconnect(&self) -> Result<(), Error>
Closes the WebSocket connection gracefully.
Sourcepub async fn reconnect(&self) -> Result<(), Error>
pub async fn reconnect(&self) -> Result<(), Error>
Attempts to reconnect to the WebSocket server.
Sourcepub async fn reconnect_with_cancel(
&self,
cancel_token: Option<CancellationToken>,
) -> Result<(), Error>
pub async fn reconnect_with_cancel( &self, cancel_token: Option<CancellationToken>, ) -> Result<(), Error>
Attempts to reconnect with cancellation support.
Sourcepub fn reconnect_count(&self) -> u32
pub fn reconnect_count(&self) -> u32
Returns the current reconnection attempt count.
Sourcepub fn reset_reconnect_count(&self)
pub fn reset_reconnect_count(&self)
Resets the reconnection attempt counter.
Sourcepub fn stats(&self) -> WsStatsSnapshot
pub fn stats(&self) -> WsStatsSnapshot
Returns a snapshot of connection statistics.
Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
Resets all connection statistics.
Sourcepub fn dropped_messages(&self) -> u32
pub fn dropped_messages(&self) -> u32
Returns the number of messages dropped due to backpressure.
This counter is incremented when the message channel is full and messages are dropped according to the configured backpressure strategy.
Sourcepub fn reset_dropped_messages(&self)
pub fn reset_dropped_messages(&self)
Resets the dropped messages counter.
Sourcepub fn create_auto_reconnect_coordinator(
self: Arc<WsClient>,
) -> AutoReconnectCoordinator
pub fn create_auto_reconnect_coordinator( self: Arc<WsClient>, ) -> AutoReconnectCoordinator
Creates an automatic reconnection coordinator.
Sourcepub async fn subscribe(
&self,
channel: String,
symbol: Option<String>,
params: Option<HashMap<String, Value>>,
) -> Result<(), Error>
pub async fn subscribe( &self, channel: String, symbol: Option<String>, params: Option<HashMap<String, Value>>, ) -> Result<(), Error>
Subscribes to a WebSocket channel.
Sourcepub async fn unsubscribe(
&self,
channel: String,
symbol: Option<String>,
) -> Result<(), Error>
pub async fn unsubscribe( &self, channel: String, symbol: Option<String>, ) -> Result<(), Error>
Unsubscribes from a WebSocket channel.
Sourcepub fn state(&self) -> WsConnectionState
pub fn state(&self) -> WsConnectionState
Returns the current connection state.
Sourcepub fn set_state(&self, state: WsConnectionState)
pub fn set_state(&self, state: WsConnectionState)
Sets the connection state.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Checks whether the WebSocket is currently connected.
Sourcepub fn is_subscribed(&self, channel: &str, symbol: Option<&String>) -> bool
pub fn is_subscribed(&self, channel: &str, symbol: Option<&String>) -> bool
Checks if subscribed to a specific channel.
Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Returns the number of active subscriptions.
Sourcepub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Returns the remaining capacity for new subscriptions.
Sourcepub fn subscriptions(&self) -> Vec<String>
pub fn subscriptions(&self) -> Vec<String>
Returns a list of all active subscription channel names.
Each subscription is identified by its channel name, optionally combined with a symbol in the format “channel:symbol” or just “channel”.
Sourcepub async fn send(&self, message: Message) -> Result<(), Error>
pub async fn send(&self, message: Message) -> Result<(), Error>
Sends a raw WebSocket message.
This method uses a bounded channel for sending. If the write channel is full, it will wait until space is available.