flare_im_core/client/
config.rs

1use std::time::Duration;
2use flare_core::flare_net::net::Platform;
3
4const PING_INTERVAL: Duration = Duration::from_secs(30);
5const PONG_TIMEOUT: Duration = Duration::from_secs(10);
6const RECONNECT_INTERVAL: Duration = Duration::from_secs(5);
7const MAX_RECONNECT_ATTEMPTS: u32 = 5;
8
9/// 客户端配置
10#[derive(Clone)]
11pub struct ClientConfig {
12    pub ping_interval: Duration,
13    pub pong_timeout: Duration,
14    pub reconnect_interval: Duration,
15    pub max_reconnect_attempts: u32,
16    pub auth_token: String,
17    pub platform: Platform,
18    pub client_id: String,
19    pub user_id: String,
20    pub language: Option<String>,
21}
22
23impl Default for ClientConfig {
24    fn default() -> Self {
25        Self {
26            ping_interval: PING_INTERVAL,
27            pong_timeout: PONG_TIMEOUT,
28            reconnect_interval: RECONNECT_INTERVAL,
29            max_reconnect_attempts: MAX_RECONNECT_ATTEMPTS,
30            auth_token: String::new(),
31            platform: Platform::Web,
32            client_id: uuid::Uuid::new_v4().to_string(),
33            user_id: String::new(),
34            language: None,
35        }
36    }
37}
38
39/// 客户端配置构建器
40pub struct ClientConfigBuilder {
41    config: ClientConfig,
42}
43
44impl ClientConfigBuilder {
45    pub fn new() -> Self {
46        Self {
47            config: ClientConfig::default(),
48        }
49    }
50
51    /// 设置心跳间隔
52    pub fn ping_interval(mut self, interval: Duration) -> Self {
53        self.config.ping_interval = interval;
54        self
55    }
56
57    /// 设置 PONG 超时时间
58    pub fn pong_timeout(mut self, timeout: Duration) -> Self {
59        self.config.pong_timeout = timeout;
60        self
61    }
62
63    /// 设置重连间隔
64    pub fn reconnect_interval(mut self, interval: Duration) -> Self {
65        self.config.reconnect_interval = interval;
66        self
67    }
68
69    /// 设置最大重连次数
70    pub fn max_reconnect_attempts(mut self, attempts: u32) -> Self {
71        self.config.max_reconnect_attempts = attempts;
72        self
73    }
74
75    /// 设置认证令牌
76    pub fn auth_token(mut self, token: impl Into<String>) -> Self {
77        self.config.auth_token = token.into();
78        self
79    }
80
81    /// 设置平台
82    pub fn platform(mut self, platform: Platform) -> Self {
83        self.config.platform = platform;
84        self
85    }
86
87    /// 设置客户端ID
88    pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
89        self.config.client_id = client_id.into();
90        self
91    }
92
93    /// 设置用户ID
94    pub fn user_id(mut self, user_id: impl Into<String>) -> Self {
95        self.config.user_id = user_id.into();
96        self
97    }
98
99    /// 设置语言
100    pub fn language(mut self, language: impl Into<String>) -> Self {
101        self.config.language = Some(language.into());
102        self
103    }
104
105    /// 构建配置
106    pub fn build(self) -> ClientConfig {
107        self.config
108    }
109}
110
111impl Default for ClientConfigBuilder {
112    fn default() -> Self {
113        Self::new()
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn test_client_config_builder() {
123        let config = ClientConfigBuilder::new()
124            .ping_interval(Duration::from_secs(60))
125            .pong_timeout(Duration::from_secs(20))
126            .reconnect_interval(Duration::from_secs(10))
127            .max_reconnect_attempts(3)
128            .auth_token("test_token")
129            .platform(Platform::Web)
130            .client_id("test_client")
131            .user_id("test_user")
132            .language("zh-CN")
133            .build();
134
135        assert_eq!(config.ping_interval, Duration::from_secs(60));
136        assert_eq!(config.pong_timeout, Duration::from_secs(20));
137        assert_eq!(config.reconnect_interval, Duration::from_secs(10));
138        assert_eq!(config.max_reconnect_attempts, 3);
139        assert_eq!(config.auth_token, "test_token");
140        assert_eq!(config.platform, Platform::Web);
141        assert_eq!(config.client_id, "test_client");
142        assert_eq!(config.user_id, "test_user");
143        assert_eq!(config.language, Some("zh-CN".to_string()));
144    }
145
146    #[test]
147    fn test_client_config_default() {
148        let config = ClientConfig::default();
149
150        assert_eq!(config.ping_interval, PING_INTERVAL);
151        assert_eq!(config.pong_timeout, PONG_TIMEOUT);
152        assert_eq!(config.reconnect_interval, RECONNECT_INTERVAL);
153        assert_eq!(config.max_reconnect_attempts, MAX_RECONNECT_ATTEMPTS);
154        assert!(config.auth_token.is_empty());
155        assert_eq!(config.platform, Platform::Unknown);
156        assert!(!config.client_id.is_empty());
157        assert!(config.user_id.is_empty());
158        assert_eq!(config.language, None);
159    }
160}