WsClient

Struct WsClient 

Source
pub struct WsClient { /* private fields */ }
Expand description

Async WebSocket client for exchange streaming APIs.

Implementations§

Source§

impl WsClient

Source

pub fn new(config: WsConfig) -> Self

Creates a new WebSocket client instance.

§Arguments
  • config - WebSocket connection configuration
§Returns

A new WsClient instance ready to connect

Source

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
Source

pub async fn disconnect(&self) -> Result<()>

Closes the WebSocket connection gracefully.

Sends shutdown signal to background tasks and clears internal state.

Source

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.

Source

pub async fn reconnect_count(&self) -> u32

Returns the current reconnection attempt count.

Source

pub async fn reset_reconnect_count(&self)

Resets the reconnection attempt counter to zero.

Source

pub async fn stats(&self) -> WsStats

Returns a snapshot of connection statistics.

Source

pub async fn reset_stats(&self)

Resets all connection statistics to default values.

Source

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.

Source

pub fn create_auto_reconnect_coordinator( self: Arc<Self>, ) -> AutoReconnectCoordinator

Creates an automatic reconnection coordinator.

§Returns

A new AutoReconnectCoordinator instance for managing reconnection logic.

Source

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 to
  • symbol - Optional trading pair symbol
  • params - Optional additional subscription parameters
§Errors

Returns error if subscription message fails to send.

Source

pub async fn unsubscribe( &self, channel: String, symbol: Option<String>, ) -> Result<()>

Unsubscribes from a WebSocket channel.

Removes subscription from internal state and sends unsubscribe message if connected.

§Arguments
  • channel - Channel name to unsubscribe from
  • symbol - Optional trading pair symbol
§Errors

Returns error if unsubscribe message fails to send.

Source

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.

Source

pub async fn state(&self) -> WsConnectionState

Returns the current connection state.

Source

pub async fn is_connected(&self) -> bool

Checks whether the WebSocket is currently connected.

Source

pub async fn send(&self, message: Message) -> Result<()>

Sends a raw WebSocket message.

§Arguments
  • message - WebSocket message to send
§Errors

Returns error if not connected or message transmission fails.

Source

pub async fn send_text(&self, text: String) -> Result<()>

Sends a text message over the WebSocket connection.

§Arguments
  • text - Text content to send
§Errors

Returns error if not connected or transmission fails.

Source

pub async fn send_json(&self, json: &Value) -> Result<()>

Sends a JSON-encoded message over the WebSocket connection.

§Arguments
  • json - JSON value to serialize and send
§Errors

Returns error if serialization fails, not connected, or transmission fails.

Auto Trait Implementations§

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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