pub struct WebsocketStreams { /* private fields */ }Implementations§
Source§impl WebsocketStreams
impl WebsocketStreams
Sourcepub fn subscribe_on_ws_events<F>(&self, callback: F) -> Subscription
pub fn subscribe_on_ws_events<F>(&self, callback: F) -> Subscription
Subscribes to WebSocket events with a provided callback function.
§Arguments
callback- A mutable function that takes aWebsocketEventand isSendand'static.
§Returns
A Subscription that can be used to manage the event subscription.
§Examples
let subscription = websocket_streams.subscribe_on_ws_events(|event| {
// Handle WebSocket event
});
Sourcepub fn unsubscribe_from_ws_events(&self, subscription: Subscription)
pub fn unsubscribe_from_ws_events(&self, subscription: Subscription)
Unsubscribes from WebSocket events for a given Subscription.
§Arguments
subscription- TheSubscriptionto unsubscribe from WebSocket events.
§Examples
let subscription = websocket_streams.subscribe_on_ws_events(|event| {
// Handle WebSocket event
});
websocket_streams.unsubscribe_from_ws_events(subscription);
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnects the WebSocket connection.
§Returns
A Result indicating whether the disconnection was successful.
Returns an error if the disconnection fails.
§Errors
Returns an anyhow::Error if the connection fails.
§Examples
let websocket_streams = WebSocketStreams::new(…);
websocket_streams.disconnect().await?;
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Sourcepub async fn ping_server(&self)
pub async fn ping_server(&self)
Sends a ping to the WebSocket server to maintain the connection.
§Examples
websocket_streams.ping_server().await;
This method sends a ping request to the WebSocket server to keep the connection alive and check the server’s responsiveness.
Sourcepub fn subscribe(&self, streams: Vec<String>, id: Option<String>)
pub fn subscribe(&self, streams: Vec<String>, id: Option<String>)
Subscribes to specified WebSocket streams.
§Arguments
streams- A vector of stream names to subscribe toid- An optional identifier for the subscription request
§Examples
websocket_streams.subscribe(vec![“btcusdt@trade".to_string()], None).await;
This method initiates an asynchronous subscription to the specified WebSocket streams.
The subscription is performed in a separate task using spawn.
Sourcepub fn unsubscribe(&self, streams: Vec<String>, id: Option<String>)
pub fn unsubscribe(&self, streams: Vec<String>, id: Option<String>)
Unsubscribes from specified WebSocket streams.
§Arguments
streams- A vector of stream names to unsubscribe fromid- An optional identifier for the unsubscription request
§Examples
websocket_streams.unsubscribe(vec![“btcusdt@trade".to_string()], None).await;
This method initiates an asynchronous unsubscription from the specified WebSocket streams.
The unsubscription is performed in a separate task using spawn.
Sourcepub async fn is_subscribed(&self, stream: &str) -> bool
pub async fn is_subscribed(&self, stream: &str) -> bool
Checks if the current WebSocket stream is subscribed to a specific stream.
§Arguments
stream- The name of the stream to check for subscription
§Returns
A boolean indicating whether the stream is currently subscribed
§Examples
let is_subscribed = websocket_streams.is_subscribed("btcusdt@trade").await;
This method checks the subscription status of a specific WebSocket stream.
Sourcepub async fn risk_data(
&self,
listen_key: String,
id: Option<String>,
) -> Result<Arc<WebsocketStream<RiskDataStreamEventsResponse>>>
pub async fn risk_data( &self, listen_key: String, id: Option<String>, ) -> Result<Arc<WebsocketStream<RiskDataStreamEventsResponse>>>
Risk Data Stream
Establishes a WebSocket stream for risk-specific data events.
§Arguments
listen_key: A unique key for identifying the risk data streamid: An optional identifier for the stream request
§Returns
Arc<WebsocketStream<RiskDataStreamEventsResponse>> on success.
§Errors
Returns an anyhow::Error if the stream creation fails or if parsing the response encounters issues.
§Examples
let risk_stream = websocket_streams.risk_data(listen_key, None).await?;
Sourcepub async fn trade_data(
&self,
listen_key: String,
id: Option<String>,
) -> Result<Arc<WebsocketStream<TradeDataStreamEventsResponse>>>
pub async fn trade_data( &self, listen_key: String, id: Option<String>, ) -> Result<Arc<WebsocketStream<TradeDataStreamEventsResponse>>>
Trade Data Stream
Establishes a WebSocket stream for trade-specific data events.
§Arguments
listen_key: A unique key for identifying the trade data streamid: An optional identifier for the stream request
§Returns
Arc<WebsocketStream<TradeDataStreamEventsResponse>> on success.
§Errors
Returns an anyhow::Error if the stream creation fails or if parsing the response encounters issues.
§Examples
let trade_stream = websocket_streams.trade_data(listen_key, None).await?;