Expand description
WebSocket connection management and message handling.
This module provides the core functionality for managing WebSocket connections, including connection lifecycle, message routing, and broadcasting capabilities.
§Overview
The connection module consists of three main components:
Connection: Represents an individual WebSocket connectionConnectionManager: Manages multiple connections with thread-safe operationshandle_websocket: Async function that handles the WebSocket lifecycle
§Architecture
Each WebSocket connection runs two concurrent tasks:
- Read task: Receives messages from the client
- Write task: Sends messages to the client via an unbounded channel
This architecture ensures that slow clients don’t block message processing.
§Examples
§Creating and Using a ConnectionManager
use wsforge::prelude::*;
use std::sync::Arc;
let manager = Arc::new(ConnectionManager::new());
// Check connection count
println!("Active connections: {}", manager.count());
// Broadcast a message to all connections
manager.broadcast(Message::text("Hello everyone!"));§Broadcasting Messages
use wsforge::prelude::*;
use std::sync::Arc;
// Broadcast to all connections
manager.broadcast(Message::text("System announcement"));
// Broadcast to all except one
manager.broadcast_except(&conn_id, Message::text("User joined"));
// Broadcast to specific connections
let target_ids = vec!["conn_1".to_string(), "conn_2".to_string()];
manager.broadcast_to(&target_ids, Message::text("Private message"));Structs§
- Connection
- Represents an active WebSocket connection.
- Connection
Info - Metadata about a WebSocket connection.
- Connection
Manager - Manages a collection of active WebSocket connections.
Functions§
- handle_
websocket - Handles the lifecycle of a WebSocket connection.
Type Aliases§
- Connection
Id - A unique identifier for a WebSocket connection.