public void HandleMessage(Action <string> logError) { if (IsBinary) { var args = new BinaryMessageReceivedEventArgs(GetBinData(), _webSocket, ConnectionId); BinaryBehavior(args); } else if (StringData != null) { var args = new StringMessageReceivedEventArgs(StringData, _webSocket, ConnectionId); StringBehavior(args); } else if (Exception != null) { logError($"Exception in read thread of connection {ConnectionId}:\r\n {ExceptionMessage}\r\n{Exception}\r\n{ (Exception.InnerException != null ? Exception.InnerException.ToString() : String.Empty) }"); } }
public virtual void OnStringMessage(StringMessageReceivedEventArgs e) { }
internal async Task RecieveLoop(WebSocket webSocket, Action <BinaryMessageReceivedEventArgs> binHandler, Action <StringMessageReceivedEventArgs> stringHandler, Action <WebSocketReceivedResultEventArgs> closeHandler, CancellationToken token = default(CancellationToken)) { MemoryStream ms = null; try { var buff = new byte[1048576]; ms = new MemoryStream(); while (webSocket.State == WebSocketState.Open) { var receivedResult = await webSocket.ReceiveAsync(new ArraySegment <byte>(buff), token); if (receivedResult.MessageType == WebSocketMessageType.Binary || receivedResult.MessageType == WebSocketMessageType.Text) { ms.Write(buff, 0, receivedResult.Count); if (receivedResult.EndOfMessage) { using (ms) { var arr = ms.ToArray(); if (receivedResult.MessageType == WebSocketMessageType.Binary) { BinaryMessageReceivedEventArgs args = new BinaryMessageReceivedEventArgs(arr, webSocket); if (_useThreadPool) { Task.Run(() => binHandler(args)); } else { binHandler(args); } } else { var args = new StringMessageReceivedEventArgs(Encoding.UTF8.GetString(arr), webSocket); if (_useThreadPool) { Task.Run(() => stringHandler(args)); } else { stringHandler(args); } } } ms = new MemoryStream(); } } else if (receivedResult.MessageType == WebSocketMessageType.Close) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); closeHandler(new WebSocketReceivedResultEventArgs(receivedResult)); } } } catch (Exception e) { _logError($"Exception: {e}"); if (webSocket.State == WebSocketState.Aborted || webSocket.State == WebSocketState.Closed) { closeHandler(new WebSocketReceivedResultEventArgs(e)); } } finally { if (ms != null) { ms.Dispose(); } } }