Struct WebsocketApi

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

Fields§

§common: Arc<WebsocketCommon>

Implementations§

Source§

impl WebsocketApi

Source

pub fn new( configuration: ConfigurationWebsocketApi, connection_pool: Vec<Arc<WebsocketConnection>>, ) -> Arc<Self>

Creates a new WebSocket API instance with the given configuration and connection pool.

§Arguments
  • configuration - Configuration settings for the WebSocket API
  • connection_pool - A vector of WebSocket connections to be used
§Returns

An Arc-wrapped WebsocketApi instance ready for use

§Panics

This function will panic if the configuration is not valid.

§Examples

let api = WebsocketApi::new(config, connection_pool);

Source

pub async fn connect(self: Arc<Self>) -> Result<(), WebsocketError>

Connects to a WebSocket server with a configurable timeout and connection handling.

This method attempts to establish a WebSocket connection if not already connected. It prevents multiple simultaneous connection attempts and supports a connection pool.

§Errors

Returns a WebsocketError if:

  • Connection fails
  • Connection times out after 10 seconds
§Behavior
  • Checks if already connected and returns early if so
  • Prevents multiple concurrent connection attempts
  • Sets a WebSocket handler for the connection pool
  • Attempts to connect with a 10-second timeout
§Returns

Ok(()) if connection is successful, otherwise a WebsocketError

Source

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

Disconnects the WebSocket connection.

§Returns

Ok(()) if disconnection is successful, otherwise a WebsocketError

§Errors

Returns a WebsocketError if:

  • Disconnection fails
  • Connection is not established
Source

pub async fn is_connected(&self) -> bool

Checks if the WebSocket connection is currently established.

§Returns

true if the connection is active, false otherwise.

Source

pub async fn ping_server(&self)

Sends a ping to the WebSocket server to maintain the connection.

This method calls the underlying connection’s ping mechanism to check and keep the WebSocket connection alive.

Source

pub async fn send_message<R>( &self, method: &str, payload: BTreeMap<String, Value>, options: WebsocketMessageSendOptions, ) -> Result<WebsocketApiResponse<R>, WebsocketError>
where R: DeserializeOwned + Send + Sync + 'static,

Sends a WebSocket message with the specified method and payload.

This method prepares and sends a WebSocket request with optional API key and signature. It handles connection status, generates a unique request ID, and processes the response.

§Arguments
  • method - The WebSocket API method to be called
  • payload - A map of parameters to be sent with the request
  • options - Configuration options for message sending (API key, signing)
§Returns

A deserialized response of type R or a WebsocketError if the request fails

§Panics

Panics if:

  • The WebSocket is not connected
  • The request cannot be processed
  • No response is received within the timeout
§Errors

Returns WebsocketError if:

  • The WebSocket is not connected
  • The request cannot be processed
  • No response is received within the timeout

Trait Implementations§

Source§

impl WebsocketHandler for WebsocketApi

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,

Callback method invoked when a WebSocket connection is successfully opened.

This method is called after a WebSocket connection is established. Currently, it does not perform any actions and serves as a placeholder for potential connection initialization or logging.

§Arguments
  • _url - The URL of the WebSocket connection that was opened
  • _connection - An Arc-wrapped WebSocket connection context
§Remarks

This method can be overridden by implementations to add custom logic when a WebSocket connection is first opened.

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 messages by parsing the JSON payload and processing pending requests.

This method is responsible for:

  • Parsing the received WebSocket message as JSON
  • Matching the message to a pending request by its ID
  • Sending the response back to the original request’s completion channel
  • Handling both successful and error responses
§Arguments
  • data - The raw WebSocket message as a string
  • connection - The WebSocket connection context associated with the message
§Behavior
  • If message parsing fails, logs an error and returns
  • For known request IDs, sends the response to the corresponding completion channel
  • Warns about responses for unknown or timed-out requests
  • Differentiates between successful (status < 400) and error responses
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,

Generates the URL to use for reconnecting to a WebSocket connection.

§Arguments
  • default_url - The original URL to potentially modify for reconnection
  • _connection - The WebSocket connection context (currently unused)
§Returns

A String representing the URL to use for reconnecting

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,