private void OnMessageReceived(byte[] bytes, WebSocketCloseStatus status, string errorMessage) { if (!disposedFlag.IsSet) { if (bytes == null) { OnFailed(status, errorMessage); } else { agent.SendCommand(this, Encoding.UTF8.GetString(bytes)); StartReceiveMessage(); } } }
private void OnMessageReceived(Task <WebSocket.Message> task) { try { var message = task.Result; if (!disposedFlag.IsSet) { if (message.IsBinary) { OnFailed(WebSocket.ErrorCode.InvalidMessageType, "Received unexpected binary message from WebSocket"); } else { agent.SendCommand(this, Encoding.UTF8.GetString(message.Payload)); StartReceiveMessage(); } } } catch (AggregateException aggregateException) { aggregateException.Handle(exception => { if (!disposedFlag.IsSet) { var webSocketException = exception as WebSocket.Exception; if (webSocketException != null) { OnFailed(webSocketException.ErrorCode, webSocketException.Message); } else { OnFailed(WebSocket.ErrorCode.ProtocolError, "Could not receive message from WebSocket"); } } return(true); }); } }