Struct WebsocketStreams

Source
pub struct WebsocketStreams {
    pub common: Arc<WebsocketCommon>,
    /* private fields */
}

Fields§

§common: Arc<WebsocketCommon>

Implementations§

Source§

impl WebsocketStreams

Source

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 streams
  • connection_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

Source

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

Source

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
Source

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.

Source

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.

Source

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 to
  • id - 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
Source

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 from
  • id - 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.

Source

pub async fn is_subscribed(&self, stream: &str) -> bool

Checks if a specific stream is currently subscribed.

§Arguments
  • stream - The stream identifier to check for subscription status
§Returns

true if the stream is subscribed, false otherwise

§Async

This method is asynchronous and requires .await when called

Trait Implementations§

Source§

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,

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,

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 string
  • connection - 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,

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 determined
  • connection - 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

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,