API Reference
Complete API documentation for WebSocket RS
WebSocketServer
The main server struct for handling WebSocket connections.
Constructor
pub fn new(addr: &str) -> Self
Creates a new WebSocket server instance.
- addr: The address to bind to (e.g., "127.0.0.1:8080")
- Returns: A new WebSocketServer instance
Methods
start
pub async fn start(&mut self, handler: F) -> Result<()>
where
F: Fn(usize, Message) -> Message + Send + 'static
Starts the WebSocket server with a message handler.
broadcast
pub async fn broadcast(&self, message: impl Into) -> Result<()>
Broadcasts a message to all connected clients.
send_to
pub async fn send_to(&self, client_id: usize, message: impl Into) -> Result<()>
Sends a message to a specific client.
on_message
pub fn on_message(&mut self, handler: F)
where
F: Fn(usize, Message) + Send + 'static
Sets the message handler callback.
on_connect
pub fn on_connect(&mut self, handler: F)
where
F: Fn(usize, &Request) -> Result<()> + Send + 'static
Sets the connection handler for authentication/validation.
on_close
pub fn on_close(&mut self, handler: F)
where
F: Fn(usize) + Send + 'static
Sets the disconnection handler.
connection_count
pub fn connection_count(&self) -> usize
Returns the number of active connections.
Message
Represents a WebSocket message.
pub enum Message {
Text(String),
Binary(Vec),
Ping(Vec),
Pong(Vec),
Close(Option),
}
Variants
- Text: A UTF-8 text message
- Binary: Binary data
- Ping: Ping frame
- Pong: Pong frame (response to ping)
- Close: Connection close frame
Config
Configuration options for the WebSocket server.
pub struct Config {
pub max_connections: usize,
pub ping_interval: Duration,
pub timeout: Duration,
pub max_message_size: usize,
pub buffer_size: usize,
}
impl Default for Config {
fn default() -> Self {
Self {
max_connections: 1000,
ping_interval: Duration::from_secs(30),
timeout: Duration::from_secs(60),
max_message_size: 64 * 1024, // 64KB
buffer_size: 8192,
}
}
}
Fields
- max_connections: Maximum number of concurrent connections
- ping_interval: Interval between ping messages
- timeout: Connection timeout duration
- max_message_size: Maximum size of a single message
- buffer_size: Size of the read/write buffer
Error Types
Error types used throughout the library.
pub enum WebSocketError {
ConnectionClosed,
InvalidMessage(String),
IoError(io::Error),
Timeout,
MaxConnectionsReached,
InvalidHandshake,
MessageTooLarge,
}
impl std::fmt::Display for WebSocketError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::ConnectionClosed => write!(f, "Connection closed"),
Self::InvalidMessage(msg) => write!(f, "Invalid message: {}", msg),
Self::IoError(e) => write!(f, "IO error: {}", e),
Self::Timeout => write!(f, "Connection timeout"),
Self::MaxConnectionsReached => write!(f, "Maximum connections reached"),
Self::InvalidHandshake => write!(f, "Invalid WebSocket handshake"),
Self::MessageTooLarge => write!(f, "Message too large"),
}
}
}
WebSocketClient
Client implementation for connecting to WebSocket servers.
Constructor
pub async fn connect(url: &str) -> Result
Connects to a WebSocket server.
Methods
send
pub async fn send(&mut self, message: impl Into) -> Result<()>
receive
pub async fn receive(&mut self) -> Result
close
pub async fn close(self) -> Result<()>
Need More Help?
Check out our comprehensive guide and examples for more detailed information.