pub struct WsClient { /* private fields */ }Expand description
Async WebSocket client for exchange streaming APIs.
Implementations§
Source§impl WsClient
impl WsClient
Sourcepub async fn connect(&self) -> Result<()>
pub async fn connect(&self) -> Result<()>
Establishes connection to the WebSocket server.
Returns immediately if already connected. Automatically starts message processing loop and resubscribes to previous channels on success.
§Errors
Returns error if:
- Connection timeout exceeded
- Network error occurs
- Server rejects connection
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Closes the WebSocket connection gracefully.
Sends shutdown signal to background tasks and clears internal state.
Sourcepub async fn reconnect(&self) -> Result<()>
pub async fn reconnect(&self) -> Result<()>
Attempts to reconnect to the WebSocket server.
Respects max_reconnect_attempts configuration and waits for
reconnect_interval before attempting connection.
§Errors
Returns error if maximum reconnection attempts exceeded or connection fails.
Sourcepub async fn reconnect_count(&self) -> u32
pub async fn reconnect_count(&self) -> u32
Returns the current reconnection attempt count.
Sourcepub async fn reset_reconnect_count(&self)
pub async fn reset_reconnect_count(&self)
Resets the reconnection attempt counter to zero.
Sourcepub async fn reset_stats(&self)
pub async fn reset_stats(&self)
Resets all connection statistics to default values.
Sourcepub async fn latency(&self) -> Option<i64>
pub async fn latency(&self) -> Option<i64>
Calculates current connection latency in milliseconds.
§Returns
Time difference between last pong and ping, or None if no data available.
Sourcepub fn create_auto_reconnect_coordinator(
self: Arc<Self>,
) -> AutoReconnectCoordinator
pub fn create_auto_reconnect_coordinator( self: Arc<Self>, ) -> AutoReconnectCoordinator
Creates an automatic reconnection coordinator.
§Returns
A new AutoReconnectCoordinator instance for managing reconnection logic.
Sourcepub async fn subscribe(
&self,
channel: String,
symbol: Option<String>,
params: Option<HashMap<String, Value>>,
) -> Result<()>
pub async fn subscribe( &self, channel: String, symbol: Option<String>, params: Option<HashMap<String, Value>>, ) -> Result<()>
Subscribes to a WebSocket channel.
Subscription is persisted and automatically reestablished on reconnection.
§Arguments
channel- Channel name to subscribe tosymbol- Optional trading pair symbolparams- Optional additional subscription parameters
§Errors
Returns error if subscription message fails to send.
Sourcepub async fn receive(&self) -> Option<Value>
pub async fn receive(&self) -> Option<Value>
Receives the next available message from the WebSocket stream.
§Returns
The received JSON message, or None if the channel is closed.
Sourcepub async fn state(&self) -> WsConnectionState
pub async fn state(&self) -> WsConnectionState
Returns the current connection state.
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Checks whether the WebSocket is currently connected.