public static void ReceiveData(Action <string> onMessage, List <byte> data) { while (data.Count > 0) { if (data[0] != Start) { throw new WebSocketException(WebSocketStatusCodes.InvalidFramePayloadData); } var endIndex = data.IndexOf(End); if (endIndex < 0) { return; } if (endIndex > MaxSize) { throw new WebSocketException(WebSocketStatusCodes.MessageTooBig); } byte[] bytes = Ex.ToArray(Ex.Take(Ex.Skip(data, 1), (endIndex - 1))); data.RemoveRange(0, endIndex + 1); var message = Encoding.UTF8.GetString(bytes); onMessage(message); } }
private void Read(List <byte> data, byte[] buffer) { if (_closed || !Socket.Connected) { return; } Socket.Receive(buffer, r => { if (r <= 0) { FleckLog.Debug("0 bytes read. Closing.", null); CloseSocket(); return; } FleckLog.Debug(r + " bytes read", null); byte[] readBytes = Ex.ToArray(Ex.Take(buffer, r)); if (Handler != null) { Handler.Receive(readBytes); } else { data.AddRange(readBytes); CreateHandler(data); } Read(data, buffer); }, HandleReadError, 0); }