创建了 Rust 版本的 CubeLib 库
This commit is contained in:
638
Windows/Rust/CubeLib/src/websocket/client.rs
Normal file
638
Windows/Rust/CubeLib/src/websocket/client.rs
Normal file
@@ -0,0 +1,638 @@
|
||||
//! WebSocket client implementation
|
||||
|
||||
use crate::websocket::{WebSocketConfig, WebSocketMessage, WebSocketError};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
use tokio::time::Duration;
|
||||
use tracing::{warn, debug};
|
||||
use serde_json::Value;
|
||||
|
||||
/// Message to send through the channel
|
||||
#[derive(Debug)]
|
||||
enum OutgoingMessage {
|
||||
/// Text message
|
||||
Text(String),
|
||||
/// Binary message
|
||||
Binary(Vec<u8>),
|
||||
/// Close connection
|
||||
Close,
|
||||
}
|
||||
|
||||
/// Connection status
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
pub enum ConnectionStatus {
|
||||
/// Disconnected
|
||||
Disconnected,
|
||||
/// Connecting
|
||||
Connecting,
|
||||
/// Connected
|
||||
Connected,
|
||||
/// Reconnecting
|
||||
Reconnecting,
|
||||
/// Error
|
||||
Error,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ConnectionStatus {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ConnectionStatus::Disconnected => write!(f, "disconnected"),
|
||||
ConnectionStatus::Connecting => write!(f, "connecting"),
|
||||
ConnectionStatus::Connected => write!(f, "connected"),
|
||||
ConnectionStatus::Reconnecting => write!(f, "reconnecting"),
|
||||
ConnectionStatus::Error => write!(f, "error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Event callback types (wrapped in Arc for cloneability)
|
||||
pub type ConnectedCallback = Arc<dyn Fn(String) + Send + Sync>;
|
||||
pub type DisconnectedCallback = Arc<dyn Fn() + Send + Sync>;
|
||||
pub type MessageCallback = Arc<dyn Fn(String, Value) + Send + Sync>;
|
||||
pub type BinaryCallback = Arc<dyn Fn(Vec<u8>) + Send + Sync>;
|
||||
pub type ErrorCallback = Arc<dyn Fn(String) + Send + Sync>;
|
||||
pub type StatusCallback = Arc<dyn Fn(ConnectionStatus, ConnectionStatus) + Send + Sync>;
|
||||
pub type SentCallback = Arc<dyn Fn(String, Value) + Send + Sync>;
|
||||
|
||||
/// WebSocket client with event-driven architecture
|
||||
pub struct WebSocketClient {
|
||||
config: WebSocketConfig,
|
||||
status: Arc<Mutex<ConnectionStatus>>,
|
||||
sender: Arc<Mutex<Option<mpsc::Sender<OutgoingMessage>>>>,
|
||||
message_queue: Arc<Mutex<Vec<OutgoingMessage>>>,
|
||||
task_handle: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
|
||||
is_running: Arc<Mutex<bool>>,
|
||||
|
||||
// Event callbacks
|
||||
on_connected: Option<ConnectedCallback>,
|
||||
on_disconnected: Option<DisconnectedCallback>,
|
||||
on_message: Option<MessageCallback>,
|
||||
on_binary: Option<BinaryCallback>,
|
||||
on_error: Option<ErrorCallback>,
|
||||
on_status_changed: Option<StatusCallback>,
|
||||
on_message_sent: Option<SentCallback>,
|
||||
|
||||
// Reconnection state
|
||||
reconnect_attempts: Arc<Mutex<u32>>,
|
||||
reconnect_delay_ms: Arc<Mutex<u64>>,
|
||||
}
|
||||
|
||||
impl WebSocketClient {
|
||||
/// Create a new WebSocket client
|
||||
pub fn new(config: WebSocketConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
status: Arc::new(Mutex::new(ConnectionStatus::Disconnected)),
|
||||
sender: Arc::new(Mutex::new(None)),
|
||||
message_queue: Arc::new(Mutex::new(Vec::new())),
|
||||
task_handle: Arc::new(Mutex::new(None)),
|
||||
is_running: Arc::new(Mutex::new(false)),
|
||||
on_connected: None,
|
||||
on_disconnected: None,
|
||||
on_message: None,
|
||||
on_binary: None,
|
||||
on_error: None,
|
||||
on_status_changed: None,
|
||||
on_message_sent: None,
|
||||
reconnect_attempts: Arc::new(Mutex::new(0)),
|
||||
reconnect_delay_ms: Arc::new(Mutex::new(1000)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a simple client with just the URL
|
||||
pub fn simple(url: impl Into<String>) -> Self {
|
||||
Self::new(WebSocketConfig::new(url))
|
||||
}
|
||||
|
||||
// ==================== Event Handlers ====================
|
||||
|
||||
/// Set callback for connected event
|
||||
pub fn on_connected<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: Fn(String) + Send + Sync + 'static,
|
||||
{
|
||||
self.on_connected = Some(Arc::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set callback for disconnected event
|
||||
pub fn on_disconnected<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: Fn() + Send + Sync + 'static,
|
||||
{
|
||||
self.on_disconnected = Some(Arc::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set callback for text message received
|
||||
pub fn on_message<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: Fn(String, Value) + Send + Sync + 'static,
|
||||
{
|
||||
self.on_message = Some(Arc::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set callback for binary message received
|
||||
pub fn on_binary<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: Fn(Vec<u8>) + Send + Sync + 'static,
|
||||
{
|
||||
self.on_binary = Some(Arc::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set callback for error
|
||||
pub fn on_error<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: Fn(String) + Send + Sync + 'static,
|
||||
{
|
||||
self.on_error = Some(Arc::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set callback for status changed
|
||||
pub fn on_status_changed<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: Fn(ConnectionStatus, ConnectionStatus) + Send + Sync + 'static,
|
||||
{
|
||||
self.on_status_changed = Some(Arc::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set callback for message sent
|
||||
pub fn on_message_sent<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: Fn(String, Value) + Send + Sync + 'static,
|
||||
{
|
||||
self.on_message_sent = Some(Arc::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
// ==================== Connection Management ====================
|
||||
|
||||
/// Connect to the WebSocket server
|
||||
pub async fn connect(&mut self) {
|
||||
let url = self.config.ws_url.clone();
|
||||
self.connect_with_url(&url).await;
|
||||
}
|
||||
|
||||
/// Connect to a specific URL (overrides config URL)
|
||||
pub async fn connect_with_url(&mut self, url: &str) {
|
||||
let _old_status = *self.status.lock().await;
|
||||
self.set_status(ConnectionStatus::Connecting).await;
|
||||
|
||||
if self.config.debug_mode {
|
||||
debug!("Connecting to WebSocket server: {}", url);
|
||||
}
|
||||
|
||||
// Create channel for outgoing messages
|
||||
let (tx, rx) = mpsc::channel::<OutgoingMessage>(100);
|
||||
*self.sender.lock().await = Some(tx);
|
||||
|
||||
// Clone shared state for the task
|
||||
let url = url.to_string();
|
||||
let config = self.config.clone();
|
||||
let status = Arc::clone(&self.status);
|
||||
let queue = Arc::clone(&self.message_queue);
|
||||
let is_running = Arc::clone(&self.is_running);
|
||||
let reconnect_attempts = Arc::clone(&self.reconnect_attempts);
|
||||
let reconnect_delay_ms = Arc::clone(&self.reconnect_delay_ms);
|
||||
|
||||
// Callbacks
|
||||
let on_connected = self.on_connected.clone();
|
||||
let on_disconnected = self.on_disconnected.clone();
|
||||
let on_message = self.on_message.clone();
|
||||
let on_binary = self.on_binary.clone();
|
||||
let on_error = self.on_error.clone();
|
||||
|
||||
// Spawn the WebSocket task
|
||||
*self.is_running.lock().await = true;
|
||||
|
||||
let task_handle = tokio::spawn(async move {
|
||||
Self::websocket_loop(
|
||||
url,
|
||||
config,
|
||||
rx,
|
||||
status,
|
||||
queue,
|
||||
is_running,
|
||||
reconnect_attempts,
|
||||
reconnect_delay_ms,
|
||||
on_connected,
|
||||
on_disconnected,
|
||||
on_message,
|
||||
on_binary,
|
||||
on_error,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
|
||||
*self.task_handle.lock().await = Some(task_handle);
|
||||
}
|
||||
|
||||
/// Main WebSocket loop
|
||||
async fn websocket_loop(
|
||||
url: String,
|
||||
config: WebSocketConfig,
|
||||
mut receiver: mpsc::Receiver<OutgoingMessage>,
|
||||
status: Arc<Mutex<ConnectionStatus>>,
|
||||
queue: Arc<Mutex<Vec<OutgoingMessage>>>,
|
||||
is_running: Arc<Mutex<bool>>,
|
||||
reconnect_attempts: Arc<Mutex<u32>>,
|
||||
reconnect_delay_ms: Arc<Mutex<u64>>,
|
||||
on_connected: Option<ConnectedCallback>,
|
||||
on_disconnected: Option<DisconnectedCallback>,
|
||||
on_message: Option<MessageCallback>,
|
||||
on_binary: Option<BinaryCallback>,
|
||||
on_error: Option<ErrorCallback>,
|
||||
) {
|
||||
loop {
|
||||
let should_run = *is_running.lock().await;
|
||||
if !should_run {
|
||||
break;
|
||||
}
|
||||
|
||||
match Self::connect_and_handle(&url, &config, &mut receiver, &queue, &on_connected, &on_message, &on_binary, &on_error).await {
|
||||
Ok(_) => {
|
||||
if config.debug_mode {
|
||||
debug!("WebSocket connection closed normally");
|
||||
}
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
if config.debug_mode {
|
||||
warn!("WebSocket error: {:?}", e);
|
||||
}
|
||||
|
||||
if let Some(ref callback) = on_error {
|
||||
callback(e.to_string());
|
||||
}
|
||||
|
||||
if config.reconnect && *is_running.lock().await {
|
||||
let delay = *reconnect_delay_ms.lock().await;
|
||||
let attempts = *reconnect_attempts.lock().await + 1;
|
||||
*reconnect_attempts.lock().await = attempts;
|
||||
|
||||
if config.debug_mode {
|
||||
debug!("Reconnecting in {}ms (attempt {})", delay, attempts);
|
||||
}
|
||||
|
||||
*status.lock().await = ConnectionStatus::Reconnecting;
|
||||
tokio::time::sleep(Duration::from_millis(delay)).await;
|
||||
|
||||
// Exponential backoff
|
||||
let new_delay = std::cmp::min(delay * 2, config.max_reconnect_delay_ms);
|
||||
*reconnect_delay_ms.lock().await = new_delay;
|
||||
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*status.lock().await = ConnectionStatus::Disconnected;
|
||||
if let Some(callback) = on_disconnected {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
/// Connect and handle messages
|
||||
async fn connect_and_handle(
|
||||
url: &str,
|
||||
config: &WebSocketConfig,
|
||||
receiver: &mut mpsc::Receiver<OutgoingMessage>,
|
||||
_queue: &Arc<Mutex<Vec<OutgoingMessage>>>,
|
||||
on_connected: &Option<ConnectedCallback>,
|
||||
on_message: &Option<MessageCallback>,
|
||||
on_binary: &Option<BinaryCallback>,
|
||||
on_error: &Option<ErrorCallback>,
|
||||
) -> Result<(), WebSocketError> {
|
||||
use http::header::{HeaderName, HeaderValue};
|
||||
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
||||
|
||||
// Build request with headers
|
||||
let mut request = url.into_client_request()?;
|
||||
|
||||
// Add client type header if specified
|
||||
if let Some(ref client_type) = config.client_type {
|
||||
request.headers_mut().insert(
|
||||
HeaderName::from_static("clienttype"),
|
||||
HeaderValue::from_str(client_type)
|
||||
.map_err(|e| WebSocketError::HeaderError(e.to_string()))?,
|
||||
);
|
||||
}
|
||||
|
||||
// Add custom headers
|
||||
for (key, value) in &config.custom_headers {
|
||||
if let (Ok(name), Ok(val)) = (
|
||||
HeaderName::try_from(key.as_str()),
|
||||
HeaderValue::from_str(value),
|
||||
) {
|
||||
request.headers_mut().insert(name, val);
|
||||
}
|
||||
}
|
||||
|
||||
// Connect with timeout
|
||||
let ws_stream = tokio::time::timeout(
|
||||
Duration::from_millis(config.connect_timeout_ms),
|
||||
tokio_tungstenite::connect_async(request),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| WebSocketError::ConnectionTimeout)?
|
||||
.map_err(|e| WebSocketError::ConnectionFailed(e.to_string()))?;
|
||||
|
||||
let (mut write, mut read) = ws_stream.0.split();
|
||||
|
||||
if config.debug_mode {
|
||||
debug!("Connected to WebSocket server");
|
||||
}
|
||||
|
||||
// Call on_connected callback
|
||||
if let Some(ref callback) = on_connected {
|
||||
callback(url.to_string());
|
||||
}
|
||||
|
||||
// Start heartbeat task if enabled
|
||||
let heartbeat_handle = if config.heartbeat_interval_ms > 0 {
|
||||
let interval_duration = Duration::from_millis(config.heartbeat_interval_ms);
|
||||
Some(tokio::spawn(async move {
|
||||
let mut ticker = tokio::time::interval(interval_duration);
|
||||
loop {
|
||||
ticker.tick().await;
|
||||
// Heartbeat will be sent via the receiver
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Handle messages
|
||||
loop {
|
||||
tokio::select! {
|
||||
// Incoming message from server
|
||||
msg = read.next() => {
|
||||
match msg {
|
||||
Some(Ok(tokio_tungstenite::tungstenite::Message::Text(text))) => {
|
||||
if config.debug_mode {
|
||||
debug!("Received text: {}", text);
|
||||
}
|
||||
|
||||
// Parse and extract type
|
||||
if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&text) {
|
||||
let msg_type = parsed.get("Type")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("unknown")
|
||||
.to_string();
|
||||
|
||||
if let Some(ref callback) = on_message {
|
||||
callback(msg_type, parsed);
|
||||
}
|
||||
} else if let Some(ref callback) = on_message {
|
||||
callback("raw".to_string(), serde_json::json!(text));
|
||||
}
|
||||
}
|
||||
Some(Ok(tokio_tungstenite::tungstenite::Message::Binary(data))) => {
|
||||
if config.debug_mode {
|
||||
debug!("Received binary: {} bytes", data.len());
|
||||
}
|
||||
if let Some(ref callback) = on_binary {
|
||||
callback(data);
|
||||
}
|
||||
}
|
||||
Some(Ok(tokio_tungstenite::tungstenite::Message::Close(_))) => {
|
||||
if config.debug_mode {
|
||||
debug!("Server initiated close");
|
||||
}
|
||||
break;
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
if config.debug_mode {
|
||||
warn!("WebSocket read error: {}", e);
|
||||
}
|
||||
if let Some(ref callback) = on_error {
|
||||
callback(e.to_string());
|
||||
}
|
||||
break;
|
||||
}
|
||||
None => {
|
||||
if config.debug_mode {
|
||||
debug!("WebSocket stream ended");
|
||||
}
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Outgoing message
|
||||
outgoing = receiver.recv() => {
|
||||
match outgoing {
|
||||
Some(OutgoingMessage::Text(text)) => {
|
||||
if config.debug_mode {
|
||||
debug!("Sending text: {}", text);
|
||||
}
|
||||
write.send(tokio_tungstenite::tungstenite::Message::Text(text))
|
||||
.await
|
||||
.map_err(|e| WebSocketError::SendFailed(e.to_string()))?;
|
||||
}
|
||||
Some(OutgoingMessage::Binary(data)) => {
|
||||
if config.debug_mode {
|
||||
debug!("Sending binary: {} bytes", data.len());
|
||||
}
|
||||
write.send(tokio_tungstenite::tungstenite::Message::Binary(data))
|
||||
.await
|
||||
.map_err(|e| WebSocketError::SendFailed(e.to_string()))?;
|
||||
}
|
||||
Some(OutgoingMessage::Close) => {
|
||||
if config.debug_mode {
|
||||
debug!("Initiating close");
|
||||
}
|
||||
write.close().await.ok();
|
||||
break;
|
||||
}
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for heartbeat handle
|
||||
if let Some(handle) = heartbeat_handle {
|
||||
handle.abort();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Disconnect from the server
|
||||
pub async fn disconnect(&mut self) {
|
||||
*self.is_running.lock().await = false;
|
||||
|
||||
if let Some(sender) = self.sender.lock().await.take() {
|
||||
let _ = sender.send(OutgoingMessage::Close).await;
|
||||
}
|
||||
|
||||
if let Some(handle) = self.task_handle.lock().await.take() {
|
||||
handle.abort();
|
||||
}
|
||||
|
||||
self.set_status(ConnectionStatus::Disconnected).await;
|
||||
}
|
||||
|
||||
// ==================== Message Sending ====================
|
||||
|
||||
/// Send a text message
|
||||
pub async fn send(&self, msg_type: &str, data: Value) -> Result<(), WebSocketError> {
|
||||
let message = WebSocketMessage::new(msg_type, data);
|
||||
let json = serde_json::to_string(&message)?;
|
||||
self.send_raw_text(json).await
|
||||
}
|
||||
|
||||
/// Send raw text
|
||||
pub async fn send_raw_text(&self, text: String) -> Result<(), WebSocketError> {
|
||||
let status = *self.status.lock().await;
|
||||
|
||||
if status == ConnectionStatus::Connected {
|
||||
if let Some(ref sender) = *self.sender.lock().await {
|
||||
sender.send(OutgoingMessage::Text(text))
|
||||
.await
|
||||
.map_err(|_| WebSocketError::NotConnected)?;
|
||||
}
|
||||
} else {
|
||||
// Queue the message
|
||||
let mut queue = self.message_queue.lock().await;
|
||||
if queue.len() < self.config.max_queue_size {
|
||||
if self.config.debug_mode {
|
||||
debug!("Queueing message (queue size: {})", queue.len() + 1);
|
||||
}
|
||||
queue.push(OutgoingMessage::Text(text));
|
||||
} else {
|
||||
if self.config.debug_mode {
|
||||
warn!("Queue full, message dropped");
|
||||
}
|
||||
return Err(WebSocketError::QueueFull);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send binary data
|
||||
pub async fn send_binary(&self, data: Vec<u8>) -> Result<(), WebSocketError> {
|
||||
let status = *self.status.lock().await;
|
||||
|
||||
if status == ConnectionStatus::Connected {
|
||||
if let Some(ref sender) = *self.sender.lock().await {
|
||||
sender.send(OutgoingMessage::Binary(data))
|
||||
.await
|
||||
.map_err(|_| WebSocketError::NotConnected)?;
|
||||
}
|
||||
} else {
|
||||
let mut queue = self.message_queue.lock().await;
|
||||
if queue.len() < self.config.max_queue_size {
|
||||
if self.config.debug_mode {
|
||||
debug!("Queueing binary message");
|
||||
}
|
||||
queue.push(OutgoingMessage::Binary(data));
|
||||
} else {
|
||||
return Err(WebSocketError::QueueFull);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ==================== Status & Queue ====================
|
||||
|
||||
/// Get current connection status
|
||||
pub async fn get_status(&self) -> ConnectionStatus {
|
||||
*self.status.lock().await
|
||||
}
|
||||
|
||||
/// Check if connected
|
||||
pub async fn is_connected(&self) -> bool {
|
||||
*self.status.lock().await == ConnectionStatus::Connected
|
||||
}
|
||||
|
||||
/// Get queue size
|
||||
pub async fn get_queue_size(&self) -> usize {
|
||||
self.message_queue.lock().await.len()
|
||||
}
|
||||
|
||||
/// Flush queued messages
|
||||
pub async fn flush_queue(&self) -> Result<(), WebSocketError> {
|
||||
let status = *self.status.lock().await;
|
||||
if status != ConnectionStatus::Connected {
|
||||
return Err(WebSocketError::NotConnected);
|
||||
}
|
||||
|
||||
let mut queue = self.message_queue.lock().await;
|
||||
if let Some(ref sender) = *self.sender.lock().await {
|
||||
while let Some(msg) = queue.pop() {
|
||||
sender.send(msg)
|
||||
.await
|
||||
.map_err(|_| WebSocketError::NotConnected)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ==================== Private Helpers ====================
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
async fn set_status(&mut self, new_status: ConnectionStatus) {
|
||||
let old_status = *self.status.lock().await;
|
||||
if old_status != new_status {
|
||||
*self.status.lock().await = new_status.clone();
|
||||
|
||||
// Reset reconnect state on successful connection
|
||||
if new_status == ConnectionStatus::Connected {
|
||||
*self.reconnect_attempts.lock().await = 0;
|
||||
*self.reconnect_delay_ms.lock().await = self.config.reconnect_delay_ms;
|
||||
}
|
||||
|
||||
if let Some(ref callback) = self.on_status_changed {
|
||||
callback(old_status, new_status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WebSocketClient {
|
||||
fn drop(&mut self) {
|
||||
// Attempt to disconnect synchronously
|
||||
let _ = tokio::runtime::Handle::current().block_on(self.disconnect());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_config_builder() {
|
||||
let config = WebSocketConfig::new("ws://localhost:8080")
|
||||
.with_client_type("Client")
|
||||
.with_debug(true)
|
||||
.with_reconnect(false);
|
||||
|
||||
assert_eq!(config.ws_url, "ws://localhost:8080");
|
||||
assert_eq!(config.client_type, Some("Client".to_string()));
|
||||
assert!(config.debug_mode);
|
||||
assert!(!config.reconnect);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_client_creation() {
|
||||
let config = WebSocketConfig::default();
|
||||
let client = WebSocketClient::new(config);
|
||||
|
||||
let status = client.get_status().await;
|
||||
assert_eq!(status, ConnectionStatus::Disconnected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user