private async Task StartListen() { var buffer = new byte[ReceiveChunkSize]; try { while (_webSocket.State == WebSocketState.Open) { var stringResult = new StringBuilder(); WebSocketReceiveResult result; do { result = await _webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); if (result.MessageType == WebSocketMessageType.Close) { await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); } else { var str = Encoding.UTF8.GetString(buffer, 0, result.Count); stringResult.Append(str); } } while (!result.EndOfMessage); var message = stringResult.ToString(); if (message.Trim() == "") { Console.WriteLine("Empty message"); } else { _webSocketListener.Message(message); } } } catch (Exception e) { Console.WriteLine(e); } finally { _webSocket.Dispose(); } }