@@ -75,7 +75,7 @@ public WebsocketClient(
7575
7676 // Start connection
7777 _ = StartConnectionAsync ( ) ;
78-
78+
7979 // Start health check
8080 _healthCheckTask = RunHealthCheckAsync ( ) ;
8181 }
@@ -85,22 +85,24 @@ private async Task StartConnectionAsync()
8585 try
8686 {
8787 _logger ? . LogInformation ( $ "WebSocket connecting to { _url } ") ;
88-
88+
8989 await Task . Run ( ( ) => _client . Start ( ) , _cancellationTokenSource . Token ) ;
90-
90+
9191 // 重置重连计数
9292 _reconnectAttempts = 0 ;
9393 }
9494 catch ( Exception e )
9595 {
9696 _logger ? . LogError ( e , "Error starting WebSocket connection" ) ;
97- OnClosed ? . Invoke ( new DisconnectionInfo
98- {
99- Type = DisconnectionType . Error ,
100- Reason = e . Message ,
101- Exception = e
102- } ) ;
103-
97+ OnClosed ? . Invoke (
98+ new DisconnectionInfo
99+ {
100+ Type = DisconnectionType . Error ,
101+ Reason = e . Message ,
102+ Exception = e ,
103+ }
104+ ) ;
105+
104106 // 启动自动重连
105107 _ = TryReconnectAsync ( ) ;
106108 }
@@ -109,11 +111,11 @@ private async Task StartConnectionAsync()
109111 private void OnServerConnected ( object ? sender , EventArgs e )
110112 {
111113 _logger ? . LogInformation ( "WebSocket connected" ) ;
112-
114+
113115 var connectionInfo = new ConnectionInfo
114116 {
115117 IsReconnect = _hasConnectedBefore ,
116- Timestamp = DateTimeOffset . Now
118+ Timestamp = DateTimeOffset . Now ,
117119 } ;
118120
119121 _hasConnectedBefore = true ;
@@ -123,14 +125,16 @@ private void OnServerConnected(object? sender, EventArgs e)
123125 private void OnServerDisconnected ( object ? sender , EventArgs e )
124126 {
125127 _logger ? . LogInformation ( "WebSocket disconnected" ) ;
126-
127- OnClosed ? . Invoke ( new DisconnectionInfo
128- {
129- Type = DisconnectionType . ByServer ,
130- Reason = "Server disconnected" ,
131- Timestamp = DateTimeOffset . Now
132- } ) ;
133-
128+
129+ OnClosed ? . Invoke (
130+ new DisconnectionInfo
131+ {
132+ Type = DisconnectionType . ByServer ,
133+ Reason = "Server disconnected" ,
134+ Timestamp = DateTimeOffset . Now ,
135+ }
136+ ) ;
137+
134138 // 只要 _shouldReconnect 为 true,就启动自动重连
135139 if ( _shouldReconnect && ! _disposed )
136140 {
@@ -157,12 +161,17 @@ private async Task TryReconnectAsync()
157161 while ( ! _disposed && _shouldReconnect )
158162 {
159163 _reconnectAttempts ++ ;
160-
161- // 计算延迟时间(指数退避,最大30秒)
162- var delay = Math . Min ( InitialReconnectDelay * ( int ) Math . Pow ( 2 , _reconnectAttempts - 1 ) , MaxReconnectDelay ) ;
163-
164- _logger ? . LogInformation ( $ "Attempting to reconnect (attempt { _reconnectAttempts } ) in { delay } ms...") ;
165-
164+
165+ // 计算延迟时间(简化的指数退避,1秒到30秒)
166+ var delay = Math . Min (
167+ InitialReconnectDelay << Math . Min ( _reconnectAttempts - 1 , 5 ) ,
168+ MaxReconnectDelay
169+ ) ;
170+
171+ _logger ? . LogInformation (
172+ $ "Attempting to reconnect (attempt { _reconnectAttempts } ) in { delay } ms..."
173+ ) ;
174+
166175 try
167176 {
168177 await Task . Delay ( delay , _cancellationTokenSource . Token ) ;
@@ -172,7 +181,9 @@ private async Task TryReconnectAsync()
172181 // 如果被取消但仍需要重连,继续尝试
173182 if ( _shouldReconnect && ! _disposed )
174183 {
175- _logger ? . LogInformation ( "Reconnect delay cancelled, but will continue trying..." ) ;
184+ _logger ? . LogInformation (
185+ "Reconnect delay cancelled, but will continue trying..."
186+ ) ;
176187 await Task . Delay ( delay ) ; // 使用不带取消令牌的版本
177188 }
178189 else
@@ -190,7 +201,7 @@ private async Task TryReconnectAsync()
190201 }
191202
192203 _logger ? . LogInformation ( $ "Reconnecting to { _url } ...") ;
193-
204+
194205 // 尝试停止现有连接(如果有)
195206 if ( _client . Connected )
196207 {
@@ -203,9 +214,9 @@ private async Task TryReconnectAsync()
203214 _logger ? . LogDebug ( stopEx , "Error stopping client before reconnect" ) ;
204215 }
205216 }
206-
217+
207218 await Task . Run ( ( ) => _client . Start ( ) ) ;
208-
219+
209220 _logger ? . LogInformation ( "Reconnected successfully" ) ;
210221 _reconnectAttempts = 0 ;
211222 break ;
@@ -232,8 +243,6 @@ private async Task TryReconnectAsync()
232243 /// </summary>
233244 private async Task RunHealthCheckAsync ( )
234245 {
235- _logger ? . LogDebug ( "Health check task started" ) ;
236-
237246 while ( ! _disposed && _shouldReconnect )
238247 {
239248 try
@@ -258,20 +267,15 @@ private async Task RunHealthCheckAsync()
258267 // 检查连接状态
259268 if ( ! _client . Connected && ! _isReconnecting )
260269 {
261- _logger ? . LogWarning ( "Health check detected disconnection, triggering reconnect..." ) ;
262270 _ = TryReconnectAsync ( ) ;
263271 }
264- else if ( _client . Connected )
265- {
266- _logger ? . LogDebug ( "Health check: Connection is healthy" ) ;
267- }
268272 }
269273 catch ( Exception ex )
270274 {
271275 _logger ? . LogError ( ex , "Error during health check" ) ;
272276 }
273277 }
274-
278+
275279 _logger ? . LogDebug ( "Health check task stopped" ) ;
276280 }
277281
@@ -343,7 +347,7 @@ public void Dispose()
343347 try
344348 {
345349 _cancellationTokenSource . Cancel ( ) ;
346-
350+
347351 if ( _client . Connected )
348352 {
349353 _client . Stop ( ) ;
0 commit comments