@@ -19,7 +19,7 @@ public class CoinApiWsClient : ICoinApiWsClient, IDisposable
19
19
private const string UrlProduction = "wss://ws.coinapi.io/" ;
20
20
21
21
private readonly string _url = UrlProduction ;
22
-
22
+
23
23
private readonly CancellationTokenSource _cts = new CancellationTokenSource ( ) ;
24
24
private readonly QueueThread < MessageData > _queueThread = null ;
25
25
@@ -39,7 +39,7 @@ public class CoinApiWsClient : ICoinApiWsClient, IDisposable
39
39
public long UnprocessedMessagesQueueSize => _queueThread . QueueSize ;
40
40
public event EventHandler < Exception > Error ;
41
41
public AutoResetEvent ConnectedEvent { get ; } = new AutoResetEvent ( false ) ;
42
- public bool IsConnected => _client ? . State == WebSocketState . Open ;
42
+ public bool IsConnected => _client ? . State == WebSocketState . Open ;
43
43
public DateTime ? ConnectedTime { get ; private set ; }
44
44
public ulong TotalBytesReceived { get ; private set ; }
45
45
public TimeSpan TotalWaitTime => _waitStopwatch . Elapsed ;
@@ -99,7 +99,7 @@ private void _queueThread_ItemDequeuedEvent(object sender, MessageData item)
99
99
return ;
100
100
}
101
101
102
- switch ( messageType )
102
+ switch ( messageType )
103
103
{
104
104
case MessageType . book :
105
105
HandleBookItem ( sender , item ) ;
@@ -259,7 +259,7 @@ private async Task Connect()
259
259
using ( var connectionCts = CancellationTokenSource . CreateLinkedTokenSource ( _cts . Token ) )
260
260
{
261
261
await HandleConnection ( connectionCts ) ;
262
-
262
+
263
263
ConnectedTime = null ;
264
264
TotalBytesReceived = 0 ;
265
265
_waitStopwatch . Reset ( ) ;
@@ -284,8 +284,8 @@ private async Task HeartbeatWatcher(ClientWebSocket client, CancellationTokenSou
284
284
if ( Interlocked . Increment ( ref _hbLastAction ) >= _hbLastActionMaxCount )
285
285
{
286
286
connectionCts . Cancel ( ) ;
287
- await client . CloseAsync ( WebSocketCloseStatus . NormalClosure ,
288
- nameof ( HeartbeatWatcher ) ,
287
+ await client . CloseAsync ( WebSocketCloseStatus . NormalClosure ,
288
+ nameof ( HeartbeatWatcher ) ,
289
289
CancellationToken . None ) ;
290
290
continue ;
291
291
}
@@ -338,9 +338,22 @@ private async Task HandleConnection(CancellationTokenSource connectionCts)
338
338
_queueThread . Enqueue ( messageData ) ;
339
339
}
340
340
}
341
- catch ( TaskCanceledException )
341
+ catch ( TaskCanceledException )
342
342
{
343
- await _client . CloseAsync ( WebSocketCloseStatus . NormalClosure , "Normal" , CancellationToken . None ) ;
343
+ try
344
+ {
345
+ if ( _client . State == WebSocketState . Open )
346
+ {
347
+ await _client . CloseAsync ( WebSocketCloseStatus . NormalClosure , "Normal" , CancellationToken . None ) ;
348
+
349
+ }
350
+
351
+ }
352
+ catch ( Exception ex )
353
+ {
354
+
355
+ OnError ( ex ) ;
356
+ }
344
357
}
345
358
catch ( Exception ex )
346
359
{
0 commit comments