pub enum MessageType {
Text(String),
Binary(Vec<u8>),
}Expand description
WebSocket client structure for handling secure WebSocket connections.
This client supports TLS/SSL secure connections and provides a simple interface for sending and receiving messages. It is optimized for performance with features like:
- Binary message support
- Connection timeout handling
- Certificate caching
- Auto-reconnection capabilities
- Optimized memory usage
§Example
ⓘ
use ws_rs::client::WebSocketClient;
use ws_rs::client::MessageType;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client with custom configuration
let mut client = WebSocketClient::builder()
.with_channel_capacity(200)
.with_connection_timeout(Duration::from_secs(10))
.with_auto_reconnect(true)
.build();
// Connect to a WebSocket server
client.connect(
"wss://127.0.0.1:9000",
"./certs",
"client_cert.pem",
"client_key.pem",
"ca_cert.pem"
).await?;
// Send a text message
client.send_message(MessageType::Text("Hello, server!".to_string())).await?;
// Send a binary message
client.send_message(MessageType::Binary(vec![1, 2, 3, 4])).await?;
// Receive a message
if let Some(response) = client.receive_message().await {
match response {
MessageType::Text(text) => println!("Received text: {}", text),
MessageType::Binary(data) => println!("Received binary data: {} bytes", data.len()),
}
}
// Close the connection
client.close().await;
Ok(())
}Message type enum for WebSocket communication
Variants§
Trait Implementations§
Source§impl Clone for MessageType
impl Clone for MessageType
Source§fn clone(&self) -> MessageType
fn clone(&self) -> MessageType
Returns a duplicate of the value. Read more
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for MessageType
impl RefUnwindSafe for MessageType
impl Send for MessageType
impl Sync for MessageType
impl Unpin for MessageType
impl UnwindSafe for MessageType
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
🔬This is a nightly-only experimental API. (
clone_to_uninit)