pub struct WebsocketApi {
pub common: Arc<WebsocketCommon>,
/* private fields */
}Fields§
§common: Arc<WebsocketCommon>Implementations§
Source§impl WebsocketApi
impl WebsocketApi
Sourcepub fn new(
configuration: ConfigurationWebsocketApi,
connection_pool: Vec<Arc<WebsocketConnection>>,
) -> Arc<Self>
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 APIconnection_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);
Sourcepub async fn connect(self: Arc<Self>) -> Result<(), WebsocketError>
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
Sourcepub async fn disconnect(&self) -> Result<(), WebsocketError>
pub async fn disconnect(&self) -> Result<(), WebsocketError>
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Checks if the WebSocket connection is currently established.
§Returns
true if the connection is active, 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 calls the underlying connection’s ping mechanism to check and keep the WebSocket connection alive.
Sourcepub async fn send_message<R>(
&self,
method: &str,
payload: BTreeMap<String, Value>,
options: WebsocketMessageSendOptions,
) -> Result<WebsocketApiResponse<R>, WebsocketError>
pub async fn send_message<R>( &self, method: &str, payload: BTreeMap<String, Value>, options: WebsocketMessageSendOptions, ) -> Result<WebsocketApiResponse<R>, WebsocketError>
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 calledpayload- A map of parameters to be sent with the requestoptions- 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
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,
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,
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 stringconnection- 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