aerosocket_client/
client.rs

1//! WebSocket client implementation for AeroSocket
2//!
3//! This module provides client functionality for WebSocket connections.
4
5use aerosocket_core::{Message, Result};
6use std::net::SocketAddr;
7
8/// WebSocket client
9#[derive(Debug)]
10pub struct Client {
11    /// Server address
12    addr: SocketAddr,
13    /// Client configuration
14    config: ClientConfig,
15}
16
17impl Client {
18    /// Create a new client
19    pub fn new(addr: SocketAddr) -> Self {
20        Self {
21            addr,
22            config: ClientConfig::default(),
23        }
24    }
25
26    /// Set client configuration
27    pub fn with_config(mut self, config: ClientConfig) -> Self {
28        self.config = config;
29        self
30    }
31
32    /// Connect to the WebSocket server
33    pub async fn connect(self) -> Result<ClientConnection> {
34        // TODO: Implement actual WebSocket handshake and connection
35        Ok(ClientConnection::new(self.addr))
36    }
37}
38
39/// Client connection
40#[derive(Debug)]
41pub struct ClientConnection {
42    /// Server address
43    #[allow(dead_code)]
44    remote_addr: SocketAddr,
45    /// Connection state
46    connected: bool,
47}
48
49impl ClientConnection {
50    /// Create a new client connection
51    pub fn new(addr: SocketAddr) -> Self {
52        Self {
53            remote_addr: addr,
54            connected: false,
55        }
56    }
57
58    /// Send a message
59    pub async fn send(&mut self, _message: Message) -> Result<()> {
60        // TODO: Implement actual message sending
61        Ok(())
62    }
63
64    /// Receive the next message
65    pub async fn next(&mut self) -> Result<Option<Message>> {
66        // TODO: Implement actual message receiving
67        Ok(None)
68    }
69
70    /// Close the connection
71    pub async fn close(&mut self, _code: Option<u16>, _reason: Option<&str>) -> Result<()> {
72        // TODO: Implement actual connection closing
73        self.connected = false;
74        Ok(())
75    }
76
77    /// Check if connected
78    pub fn is_connected(&self) -> bool {
79        self.connected
80    }
81}
82
83/// Client configuration
84#[derive(Debug, Clone)]
85pub struct ClientConfig {
86    /// Maximum frame size
87    pub max_frame_size: usize,
88    /// Maximum message size
89    pub max_message_size: usize,
90    /// Handshake timeout
91    pub handshake_timeout: std::time::Duration,
92    /// Enable compression
93    pub compression_enabled: bool,
94}
95
96impl Default for ClientConfig {
97    fn default() -> Self {
98        Self {
99            max_frame_size: 1024 * 1024,        // 1MB
100            max_message_size: 16 * 1024 * 1024, // 16MB
101            handshake_timeout: std::time::Duration::from_secs(30),
102            compression_enabled: false,
103        }
104    }
105}
106
107/// Client builder
108#[derive(Debug)]
109pub struct ClientBuilder {
110    addr: SocketAddr,
111    config: ClientConfig,
112}
113
114impl ClientBuilder {
115    /// Create a new client builder
116    pub fn new(addr: SocketAddr) -> Self {
117        Self {
118            addr,
119            config: ClientConfig::default(),
120        }
121    }
122
123    /// Set maximum frame size
124    pub fn max_frame_size(mut self, size: usize) -> Self {
125        self.config.max_frame_size = size;
126        self
127    }
128
129    /// Set maximum message size
130    pub fn max_message_size(mut self, size: usize) -> Self {
131        self.config.max_message_size = size;
132        self
133    }
134
135    /// Set handshake timeout
136    pub fn handshake_timeout(mut self, timeout: std::time::Duration) -> Self {
137        self.config.handshake_timeout = timeout;
138        self
139    }
140
141    /// Enable/disable compression
142    pub fn compression(mut self, enabled: bool) -> Self {
143        self.config.compression_enabled = enabled;
144        self
145    }
146
147    /// Build the client
148    pub fn build(self) -> Client {
149        Client::new(self.addr).with_config(self.config)
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    #[test]
158    fn test_client_creation() {
159        let addr = "127.0.0.1:8080".parse().unwrap();
160        let client = Client::new(addr);
161        assert_eq!(client.addr, addr);
162    }
163
164    #[test]
165    fn test_client_config() {
166        let config = ClientConfig::default();
167        assert_eq!(config.max_frame_size, 1024 * 1024);
168        assert_eq!(config.max_message_size, 16 * 1024 * 1024);
169    }
170
171    #[test]
172    fn test_client_builder() {
173        let addr = "127.0.0.1:8080".parse().unwrap();
174        let client = ClientBuilder::new(addr)
175            .max_frame_size(2048)
176            .compression(true)
177            .build();
178
179        assert_eq!(client.addr, addr);
180        assert_eq!(client.config.max_frame_size, 2048);
181        assert!(client.config.compression_enabled);
182    }
183}