public async Task StartSendingNotifications() { try { while (_cancellationTokenSource.IsCancellationRequested == false) { var result = await _manualResetEvent.WaitAsync(TimeSpan.FromMilliseconds(5000)).ConfigureAwait(false); if (IsAlive == false) { return; } if (result == false) { await SendMessage(WebSocketHelper.Heartbeat).ConfigureAwait(false); continue; } _manualResetEvent.Reset(); while (_messages.TryDequeue(out var message)) { if (IsAlive == false) { return; } await SendMessage(ToByteArraySegment(message)).ConfigureAwait(false); } } } catch (Exception e) { if (Logger.IsInfoEnabled) { Logger.Info("Error when handling web socket connection", e); } } finally { TrafficWatchManager.Disconnect(this); try { if (_disposed == false) { await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "NORMAL_CLOSE", _cancellationTokenSource.Token); } } catch { // ignore } } }
public async Task StartSendingNotifications() { try { while (_cancellationTokenSource.IsCancellationRequested == false) { var result = await _manualResetEvent.WaitAsync(TimeSpan.FromMilliseconds(5000)).ConfigureAwait(false); if (_cancellationTokenSource.IsCancellationRequested) { break; } if (result == false) { await SendMessage(HeartbeatMessage).ConfigureAwait(false); continue; } _manualResetEvent.Reset(); TrafficWatchNotification message; while (_msgs.TryDequeue(out message)) { if (_cancellationTokenSource.IsCancellationRequested) { break; } await SendMessage(ToByteArraySegment(message)).ConfigureAwait(false); } } } catch (Exception e) { Logger.Info("Error when handling web socket connection", e); _cancellationTokenSource.Cancel(); } finally { TrafficWatchManager.Disconnect(this); try { await _websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "NORNAL_CLOSE", _cancellationTokenSource?.Token ?? CancellationToken.None); } catch { // ignore } } }