Files
JoyD/Windows/Rust/CubeLib/src/websocket/config.rs

145 lines
3.7 KiB
Rust

//! WebSocket configuration
use serde::{Deserialize, Serialize};
/// WebSocket client configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSocketConfig {
/// WebSocket server URL (e.g., "ws://localhost:8087/ws")
pub ws_url: String,
/// Auto-connect on initialization
#[serde(default = "default_true")]
pub auto_connect: bool,
/// Auto-reconnect on disconnection
#[serde(default = "default_true")]
pub reconnect: bool,
/// Maximum reconnect delay in milliseconds
#[serde(default = "default_max_reconnect_delay")]
pub max_reconnect_delay_ms: u64,
/// Initial reconnect delay in milliseconds
#[serde(default = "default_reconnect_delay")]
pub reconnect_delay_ms: u64,
/// Debug mode (enable logging)
#[serde(default)]
pub debug_mode: bool,
/// Heartbeat interval in milliseconds (0 to disable)
#[serde(default = "default_heartbeat_interval")]
pub heartbeat_interval_ms: u64,
/// Connection timeout in milliseconds
#[serde(default = "default_connect_timeout")]
pub connect_timeout_ms: u64,
/// Maximum message queue size
#[serde(default = "default_max_queue_size")]
pub max_queue_size: usize,
/// Custom HTTP headers
#[serde(default)]
pub custom_headers: Vec<(String, String)>,
/// Client type for identification
#[serde(default)]
pub client_type: Option<String>,
}
fn default_true() -> bool {
true
}
fn default_max_reconnect_delay() -> u64 {
30000
}
fn default_reconnect_delay() -> u64 {
1000
}
fn default_heartbeat_interval() -> u64 {
30000
}
fn default_connect_timeout() -> u64 {
10000
}
fn default_max_queue_size() -> usize {
100
}
impl Default for WebSocketConfig {
fn default() -> Self {
Self {
ws_url: "ws://localhost:8087/ws".to_string(),
auto_connect: true,
reconnect: true,
max_reconnect_delay_ms: default_max_reconnect_delay(),
reconnect_delay_ms: default_reconnect_delay(),
debug_mode: false,
heartbeat_interval_ms: default_heartbeat_interval(),
connect_timeout_ms: default_connect_timeout(),
max_queue_size: default_max_queue_size(),
custom_headers: Vec::new(),
client_type: None,
}
}
}
impl WebSocketConfig {
/// Create a new config with the given URL
pub fn new(ws_url: impl Into<String>) -> Self {
Self {
ws_url: ws_url.into(),
..Default::default()
}
}
/// Set client type
pub fn with_client_type(mut self, client_type: impl Into<String>) -> Self {
self.client_type = Some(client_type.into());
self
}
/// Set debug mode
pub fn with_debug(mut self, debug: bool) -> Self {
self.debug_mode = debug;
self
}
/// Add a custom header
pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.custom_headers.push((key.into(), value.into()));
self
}
/// Enable auto-reconnect
pub fn with_reconnect(mut self, enabled: bool) -> Self {
self.reconnect = enabled;
self
}
/// Set heartbeat interval (0 to disable)
pub fn with_heartbeat(mut self, interval_ms: u64) -> Self {
self.heartbeat_interval_ms = interval_ms;
self
}
/// Set initial reconnect delay in milliseconds
pub fn with_reconnect_delay(mut self, delay_ms: u64) -> Self {
self.reconnect_delay_ms = delay_ms;
self
}
/// Set maximum reconnect delay in milliseconds
pub fn with_max_reconnect_delay(mut self, delay_ms: u64) -> Self {
self.max_reconnect_delay_ms = delay_ms;
self
}
}