Expand description
§atomic_websocket
atomic_websocket
is a high-level WebSocket client and server implementation for Rust built on top of tokio-tungstenite.
It provides resilient WebSocket connections with the following features:
- Automatic connection recovery and ping/pong handling
- Local network server auto-discovery
- Connection status monitoring and events
- Serialization/deserialization support
§Basic Usage
§Client Example
use atomic_websocket::{
AtomicWebsocket,
server_sender::{ClientOptions, SenderStatus},
schema::ServerConnectInfo,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure client options
let mut client_options = ClientOptions::default();
client_options.retry_seconds = 2;
client_options.use_keep_ip = true;
// Initialize DB (implementation details omitted)
let db = initialize_database().await?;
let server_sender = initialize_server_sender().await?;
// Create client
let atomic_client = AtomicWebsocket::get_internal_client_with_server_sender(
db.clone(),
client_options,
server_sender.clone(),
).await;
// Connect to server
let result = atomic_client
.get_internal_connect(
Some(ServerConnectInfo {
server_ip: "",
port: "9000",
}),
db.clone(),
)
.await;
Ok(())
}
§Server Example
use atomic_websocket::{
AtomicWebsocket,
client_sender::ServerOptions,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure server options
let options = ServerOptions::default();
// Initialize client senders for managing connections
let client_senders = initialize_client_senders().await?;
// Create and start server
let address = "0.0.0.0:9000";
let atomic_server = AtomicWebsocket::get_internal_server_with_client_senders(
address.to_string(),
options,
client_senders.clone(),
).await;
// Set up message handler
let handle_message_receiver = atomic_server.get_handle_message_receiver().await;
tokio::spawn(handle_messages(handle_message_receiver));
Ok(())
}
Modules§
- client_
sender - Module providing functionality for managing client connections on the server side.
- common
- Module providing common utility functions for WebSocket communication.
- external
- Module that re-exports various external dependencies.
- schema
- Module containing message schema definitions.
- server_
sender - Module providing functionality for managing server connections on the client side.
- types
- Module containing common type definitions used throughout the library.
Macros§
Structs§
- Atomic
Websocket - Primary entry point for creating WebSocket clients and servers.
- Settings
- Database model for storing client settings and state.
Enums§
- Atomic
Websocket Type - Internal enum to distinguish WebSocket connection types.