pub struct WebSocketClient { /* private fields */ }Implementations§
Source§impl WebSocketClient
impl WebSocketClient
Sourcepub fn new() -> Self
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.
Sourcepub fn builder() -> WebSocketClientBuilder
pub fn builder() -> WebSocketClientBuilder
Sourcepub 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>>
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 filesclient_cert_file- Client certificate filenameclient_key_file- Client private key filenameca_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.
Sourcepub async fn send_message(
&self,
message: MessageType,
) -> Result<(), Box<dyn Error>>
pub async fn send_message( &self, message: MessageType, ) -> Result<(), Box<dyn Error>>
Sourcepub async fn receive_message(&mut self) -> Option<MessageType>
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
Sourcepub async fn receive_message_timeout(
&mut self,
timeout_duration: Duration,
) -> Result<Option<MessageType>, Elapsed>
pub async fn receive_message_timeout( &mut self, timeout_duration: Duration, ) -> Result<Option<MessageType>, Elapsed>
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Checks if the client is connected to a WebSocket server.
§Returns
true if connected, false otherwise.
Sourcepub async fn close(&mut self)
pub async fn close(&mut self)
Closes the WebSocket connection.
This method gracefully shuts down the connection by:
- Dropping the sender channel to trigger closing the WebSocket
- Waiting for the worker task to complete
- Cleaning up resources
The client can be reconnected after closing by calling connect() again.
Sourcepub async fn ping(&self) -> Result<(), Box<dyn Error>>
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.
Sourcepub async fn clear_cert_cache(&self)
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.
Sourcepub async fn check_connection(&self) -> bool
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.