WebSocketClient

Struct WebSocketClient 

Source
pub struct WebSocketClient { /* private fields */ }

Implementations§

Source§

impl WebSocketClient

Source

pub fn new() -> Self

Creates a new WebSocketClient instance with default configuration.

The new client is initially disconnected. Use the connect method to establish a connection to a WebSocket server.

§Returns

A new WebSocketClient instance.

Source

pub fn builder() -> WebSocketClientBuilder

Creates a builder for configuring a WebSocketClient.

§Returns

A WebSocketClientBuilder instance.

Source

pub async fn connect( &mut self, server_url: &str, cert_dir: &str, client_cert_file: &str, client_key_file: &str, ca_cert_file: &str, ) -> Result<(), Box<dyn Error>>

Connects to a WebSocket server using TLS.

This method establishes a secure WebSocket connection to the specified server URL using the provided certificates and keys.

§Parameters
  • server_url - The WebSocket server URL (e.g., “wss://example.com:9000”)
  • cert_dir - Directory containing the certificate files
  • client_cert_file - Client certificate filename
  • client_key_file - Client private key filename
  • ca_cert_file - CA certificate filename
§Returns

Ok(()) on successful connection, or an error if the connection fails.

§Errors

Returns an error if URL parsing fails, certificate loading fails, or connection fails.

Source

pub async fn reconnect(&mut self) -> Result<(), Box<dyn Error>>

Reconnects to the WebSocket server using the last connection parameters.

§Returns

Ok(()) on successful reconnection, or an error if reconnection fails.

§Errors

Returns an error if no previous connection exists or if reconnection fails.

Source

pub async fn send_message( &self, message: MessageType, ) -> Result<(), Box<dyn Error>>

Sends a message to the connected WebSocket server.

§Parameters
  • message - The message to send (text or binary)
§Returns

Ok(()) if the message was queued for sending, or an error if not connected.

§Errors

Returns an error if the client is not connected or if the message cannot be sent.

Source

pub async fn send_text(&self, text: String) -> Result<(), Box<dyn Error>>

Sends a text message to the connected WebSocket server.

This is a convenience method that wraps send_message.

§Parameters
  • text - The text message to send
§Returns

Ok(()) if the message was queued for sending, or an error if not connected.

Source

pub async fn send_binary(&self, data: Vec<u8>) -> Result<(), Box<dyn Error>>

Sends a binary message to the connected WebSocket server.

This is a convenience method that wraps send_message.

§Parameters
  • data - The binary data to send
§Returns

Ok(()) if the message was queued for sending, or an error if not connected.

Source

pub async fn receive_message(&mut self) -> Option<MessageType>

Receives a message from the WebSocket server.

This method waits for the next message from the server. If no message is available or the connection is closed, it returns None.

§Returns
  • Some(MessageType) - The received message (text or binary)
  • None - If not connected or the connection was closed
Source

pub async fn receive_message_timeout( &mut self, timeout_duration: Duration, ) -> Result<Option<MessageType>, Elapsed>

Receives a message with timeout.

This method waits for the next message from the server with a timeout.

§Parameters
  • timeout_duration - Maximum time to wait for a message
§Returns
  • Ok(Some(MessageType)) - A message was received
  • Ok(None) - No message received (not connected)
  • Err(_) - Timeout occurred
Source

pub fn is_connected(&self) -> bool

Checks if the client is connected to a WebSocket server.

§Returns

true if connected, false otherwise.

Source

pub async fn close(&mut self)

Closes the WebSocket connection.

This method gracefully shuts down the connection by:

  1. Dropping the sender channel to trigger closing the WebSocket
  2. Waiting for the worker task to complete
  3. Cleaning up resources

The client can be reconnected after closing by calling connect() again.

Source

pub async fn ping(&self) -> Result<(), Box<dyn Error>>

Sends a ping message to check connection health.

This method can be used to keep the connection alive or check if the server is still responsive.

§Returns

Ok(()) if the ping was sent, or an error if not connected.

Source

pub async fn clear_cert_cache(&self)

Clears the certificate cache.

This method can be useful to force reloading of certificates if they have been updated on disk.

Source

pub async fn check_connection(&self) -> bool

Checks if a connection is active and sends a ping to verify connectivity.

Returns true if the connection is active and responsive.

Source

pub fn get_config(&self) -> &WSClientConfig

Gets the current configuration.

§Returns

A reference to the current client configuration.

Trait Implementations§

Source§

impl Drop for WebSocketClient

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

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> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

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> ErasedDestructor for T
where T: 'static,