On GitHub there is a repository with implementation of decoding frames of the WebSocket protocol. The problem is that if many messages are sent at one point in time, the browser sticks together frames and the code does not work correctly.
private String DecodeMessageFromClient(Byte[] bytes) { try { String incomingData = String.Empty; Byte secondByte = bytes[1]; Int32 dataLength = secondByte & 127; Int32 indexFirstMask = 2; if (dataLength == 126) indexFirstMask = 4; else if (dataLength == 127) indexFirstMask = 10; IEnumerable<Byte> keys = bytes.Skip(indexFirstMask).Take(4); Int32 indexFirstDataByte = indexFirstMask + 4; Byte[] decoded = new Byte[bytes.Length - indexFirstDataByte]; for (Int32 i = indexFirstDataByte, j = 0; i < bytes.Length; i++, j++) { decoded[j] = (Byte)(bytes[i] ^ keys.ElementAt(j % 4)); } return incomingData = Encoding.UTF8.GetString(decoded, 0, decoded.Length); } catch (Exception ex) { Debug.WriteLine("Could not decode due to :" + ex.Message); } return null; } Based on the names of variables, this happens because the code skips the header, and considers the rest of the information as the content of the frame. But since the message has two frames, the second header is not skipped.
for (Int32 i = indexFirstDataByte, j = 0; i < bytes.Length; i++, j++) 