pub struct WebsocketStreams {
pub common: Arc<WebsocketCommon>,
/* private fields */
}Fields§
§common: Arc<WebsocketCommon>Implementations§
Source§impl WebsocketStreams
impl WebsocketStreams
Sourcepub fn new(
configuration: ConfigurationWebsocketStreams,
connection_pool: Vec<Arc<WebsocketConnection>>,
) -> Arc<Self>
pub fn new( configuration: ConfigurationWebsocketStreams, connection_pool: Vec<Arc<WebsocketConnection>>, ) -> Arc<Self>
Creates a new WebsocketStreams instance with the given configuration and connection pool.
§Arguments
configuration- Configuration settings for the WebSocket streamsconnection_pool- A vector of WebSocket connections to use
§Returns
An Arc-wrapped WebsocketStreams instance
§Panics
Panics if the reconnect_delay cannot be converted to usize
Sourcepub async fn connect(
self: Arc<Self>,
streams: Vec<String>,
) -> Result<(), WebsocketError>
pub async fn connect( self: Arc<Self>, streams: Vec<String>, ) -> Result<(), WebsocketError>
Establishes a WebSocket connection for the given streams.
This method attempts to connect to a WebSocket server using the connection pool. If a connection is already established or in progress, it returns immediately.
§Arguments
streams- A vector of stream identifiers to connect to
§Returns
A Result indicating whether the connection was successful or an error occurred
§Errors
Returns a WebsocketError if the connection fails or times out after 10 seconds
Sourcepub async fn disconnect(&self) -> Result<(), WebsocketError>
pub async fn disconnect(&self) -> Result<(), WebsocketError>
Disconnects all WebSocket connections and clears associated state.
§Returns
A Result indicating whether the disconnection was successful or an error occurred
§Errors
Returns a WebsocketError if there are issues during the disconnection process
§Side Effects
- Clears stream callbacks for all connections
- Clears pending subscriptions for all connections
- Removes all connection stream mappings
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Checks if the WebSocket connection is currently active.
§Returns
true if the WebSocket connection is established, false otherwise.
Sourcepub async fn ping_server(&self)
pub async fn ping_server(&self)
Sends a ping to the WebSocket server to maintain the connection.
This method delegates the ping operation to the underlying common WebSocket connection. It is typically used to keep the connection alive and check its status.
§Side Effects
Sends a ping request to the WebSocket server through the common connection.
Sourcepub async fn subscribe(
self: Arc<Self>,
streams: Vec<String>,
id: Option<String>,
)
pub async fn subscribe( self: Arc<Self>, streams: Vec<String>, id: Option<String>, )
Subscribes to multiple WebSocket streams, handling connection and queuing logic.
§Arguments
streams- A vector of stream names to subscribe toid- An optional request identifier for the subscription
§Behavior
- Filters out streams already subscribed
- Assigns streams to appropriate connections
- Handles subscription for active connections
- Queues subscriptions for inactive connections
§Side Effects
- Sends subscription payloads for active connections
- Adds pending subscriptions for inactive connections
Sourcepub async fn unsubscribe(&self, streams: Vec<String>, id: Option<String>)
pub async 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 request identifier for the unsubscription
§Behavior
- Validates the request identifier or generates a random one
- Checks for active connections and subscribed streams
- Sends unsubscribe payload for streams with active callbacks
- Removes stream from connection streams and callbacks
§Side Effects
- Sends unsubscribe request to WebSocket server
- Removes stream tracking from internal state
§Async
This method is asynchronous and requires .await when called
§Panics
This method may panic if the request identifier is not valid.
Sourcepub async fn is_subscribed(&self, stream: &str) -> bool
pub async fn is_subscribed(&self, stream: &str) -> bool
Trait Implementations§
Source§impl WebsocketHandler for WebsocketStreams
impl WebsocketHandler for WebsocketStreams
Source§fn on_open<'life0, 'async_trait>(
&'life0 self,
_url: String,
connection: Arc<WebsocketConnection>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_open<'life0, 'async_trait>(
&'life0 self,
_url: String,
connection: Arc<WebsocketConnection>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handles the WebSocket connection opening by processing any pending subscriptions.
This method is called when a WebSocket connection is established. It retrieves
any pending stream subscriptions from the connection state and sends them
immediately using the send_subscription_payload method.
§Arguments
_url- The URL of the WebSocket connection (unused)connection- The WebSocket connection that has just been opened
§Remarks
If there are any pending subscriptions, they are sent as a batch subscription payload. The method uses a lock to safely access and clear the pending subscriptions from the connection state.
Source§fn on_message<'life0, 'async_trait>(
&'life0 self,
data: String,
connection: Arc<WebsocketConnection>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_message<'life0, 'async_trait>(
&'life0 self,
data: String,
connection: Arc<WebsocketConnection>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handles incoming WebSocket stream messages by parsing the JSON payload and invoking registered stream callbacks.
This method processes WebSocket messages with a specific structure, extracting the stream name and data. It retrieves and executes any registered callbacks associated with the stream name.
§Arguments
data- The raw WebSocket message as a JSON-formatted stringconnection- The WebSocket connection through which the message was received
§Behavior
- Parses the JSON message
- Extracts the stream name and data payload
- Looks up and invokes any registered callbacks for the stream
- Silently returns if message parsing or stream extraction fails
Source§fn get_reconnect_url<'life0, 'async_trait>(
&'life0 self,
_default_url: String,
connection: Arc<WebsocketConnection>,
) -> Pin<Box<dyn Future<Output = String> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_reconnect_url<'life0, 'async_trait>(
&'life0 self,
_default_url: String,
connection: Arc<WebsocketConnection>,
) -> Pin<Box<dyn Future<Output = String> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Retrieves the reconnection URL for a specific WebSocket connection by identifying all streams associated with that connection.
§Arguments
_default_url- A default URL that can be used if no specific reconnection URL is determinedconnection- The WebSocket connection for which to generate a reconnection URL
§Returns
A URL string that can be used to reconnect to the WebSocket, based on the streams associated with the given connection