private void ListenWorkerThread() { ArraySegment <byte> receiveBuffer = new ArraySegment <byte>(new byte[receiveChunkSize]); bool wasClosed = true; TimeSpan keepAlive = webSocket.Options.KeepAliveInterval; MemoryStream stream = new MemoryStream(); WebSocketReceiveResult result; while (!disposed) { try { if (wasClosed) { // re-open the socket wasClosed = false; webSocket = new ClientWebSocket(); webSocket.ConnectAsync(uri, CancellationToken.None).GetAwaiter().GetResult(); QueueActionWithNoExceptions(onConnected); } while (webSocket.State == WebSocketState.Open) { do { result = webSocket.ReceiveAsync(receiveBuffer, cancellationToken).GetAwaiter().GetResult(); if (result.MessageType == WebSocketMessageType.Close) { webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).GetAwaiter().GetResult(); QueueAction(onDisconnected); } else { stream.Write(receiveBuffer.Array, 0, result.Count); } }while (!result.EndOfMessage); if (stream.Length != 0) { // make a copy of the bytes, the memory stream will be re-used and could potentially corrupt in multi-threaded environments byte[] bytesCopy = new byte[stream.Length]; Array.Copy(stream.GetBuffer(), bytesCopy, stream.Length); stream.SetLength(0); messageQueue.Add(bytesCopy); } } } catch { QueueAction(onDisconnected); if (!disposed) { // wait one half second before attempting reconnect Task.Delay(500).ConfigureAwait(false).GetAwaiter().GetResult(); } } finally { wasClosed = true; try { webSocket.Dispose(); } catch { } } } }
private async Task ListenWorkerThread() { ArraySegment <byte> receiveBuffer = new ArraySegment <byte>(new byte[receiveChunkSize]); TimeSpan keepAlive = webSocket.Options.KeepAliveInterval; MemoryStream stream = new MemoryStream(); WebSocketReceiveResult result; while (!disposed) { try { // open the socket webSocket.Options.KeepAliveInterval = this.keepAlive; await webSocket.ConnectAsync(uri, cancellationToken); // on connect may make additional calls that must succeed, such as rest calls // for lists, etc. QueueActionWithNoExceptions(onConnected); while (webSocket.State == WebSocketState.Open) { do { result = await webSocket.ReceiveAsync(receiveBuffer, cancellationToken); if (result.MessageType == WebSocketMessageType.Close) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, cancellationToken); QueueAction(onDisconnected); } else { stream.Write(receiveBuffer.Array, 0, result.Count); } }while (!result.EndOfMessage); if (stream.Length != 0) { // make a copy of the bytes, the memory stream will be re-used and could potentially corrupt in multi-threaded environments byte[] bytesCopy = new byte[stream.Length]; Array.Copy(stream.GetBuffer(), bytesCopy, stream.Length); stream.SetLength(0); messageQueue.Add(bytesCopy); } } } catch { // eat exceptions, most likely a result of a disconnect, either way we will re-create the web socket } QueueAction(onDisconnected); try { webSocket.Dispose(); } catch { } if (!disposed) { // wait one second before attempting reconnect webSocket = new ClientWebSocket(); await Task.Delay(1000); } } }
private void ListenWorkerThread() { ArraySegment <byte> receiveBuffer = new ArraySegment <byte>(new byte[receiveChunkSize]); bool wasClosed = true; TimeSpan keepAlive = _ws.Options.KeepAliveInterval; MemoryStream stream = new MemoryStream(); WebSocketReceiveResult result; while (!_disposed) { try { if (wasClosed) { // re-open the socket wasClosed = false; _ws = new ClientWebSocket(); _ws.ConnectAsync(_uri, CancellationToken.None).GetAwaiter().GetResult(); QueueActionWithNoExceptions(_onConnected); } while (_ws.State == WebSocketState.Open) { do { result = _ws.ReceiveAsync(receiveBuffer, _cancellationToken).GetAwaiter().GetResult(); if (result.MessageType == WebSocketMessageType.Close) { _ws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).GetAwaiter().GetResult(); QueueAction(_onDisconnected); } else { stream.Write(receiveBuffer.Array, 0, result.Count); } }while (!result.EndOfMessage); if (stream.Length != 0) { string messageString = Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length); stream.SetLength(0); _messageQueue.Add(messageString); } } } catch { QueueAction(_onDisconnected); if (!_disposed) { // wait one half second before attempting reconnect Task.Delay(500).Wait(); } } finally { wasClosed = true; try { _ws.Dispose(); } catch { } } } }