private async Task HandleConnection() { using (_client = new ClientWebSocket()) { try { await _client.ConnectAsync(new Uri(_url), _cts.Token); var helloMsg = new ArraySegment <byte>(JsonSerializer.Serialize(helloMessage.Value)); await _client.SendAsync(helloMsg, WebSocketMessageType.Text, true, _cts.Token); while (_client.State == WebSocketState.Open && !_cts.IsCancellationRequested) { var messageData = await WSUtils.ReceiveMessage(_client); if (messageData.MessageType == WebSocketMessageType.Close) { return; } _queueThread.Enqueue(messageData); } } catch { } } }
private async Task HandleConnection(CancellationTokenSource connectionCts) { Interlocked.Exchange(ref _hbLastAction, 0); using (_client = new ClientWebSocket()) { try { _ = Task.Run(() => HeartbeatWatcher(_client, connectionCts)); await _client.ConnectAsync(new Uri(_url), connectionCts.Token); ConnectedTime = DateTime.UtcNow; ConnectedEvent.Set(); ConnectedEvent.Reset(); Interlocked.Exchange(ref _hbLastAction, 0); var currentHello = HelloMessage; var helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); Interlocked.Exchange(ref _hbLastAction, 0); var bufferArray = new byte[ReceiveBufferSize]; while (_client.State == WebSocketState.Open && !connectionCts.IsCancellationRequested) { if (currentHello != HelloMessage) { currentHello = HelloMessage; helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); Interlocked.Exchange(ref _hbLastAction, 0); } var messageData = await WSUtils.ReceiveMessage(_client, connectionCts.Token, bufferArray); Interlocked.Exchange(ref _hbLastAction, 0); if (messageData.MessageType == WebSocketMessageType.Close) { return; } _queueThread.Enqueue(messageData); } } catch (TaskCanceledException) { await _client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal", CancellationToken.None); } catch (Exception ex) { OnError(ex); } } }
private async Task HandleConnection(CancellationTokenSource connectionCts) { _hbLastAction = DateTime.UtcNow; using (_client = new ClientWebSocket()) { try { _ = Task.Run(() => HeartbeatWatcher(_client, connectionCts)); await _client.ConnectAsync(new Uri(_url), connectionCts.Token); ConnectedEvent.Set(); ConnectedEvent.Reset(); _hbLastAction = DateTime.UtcNow; var currentHello = HelloMessage; var helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); _hbLastAction = DateTime.UtcNow; while (_client.State == WebSocketState.Open && !connectionCts.IsCancellationRequested) { if (currentHello != HelloMessage) { currentHello = HelloMessage; helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); _hbLastAction = DateTime.UtcNow; } var messageData = await WSUtils.ReceiveMessage(_client, connectionCts.Token); _hbLastAction = DateTime.UtcNow; if (messageData.MessageType == WebSocketMessageType.Close) { return; } _queueThread.Enqueue(messageData); } } catch (TaskCanceledException) { } catch (Exception ex) { OnError(ex); } } }