Exemplo n.º 1
0
        public byte[] ToArray()
        {
            using (var buff = new MemoryStream())
            {
                var header = (int)Fin;
                header = (header << 1) + (int)Rsv2;
                header = (header << 1) + (int)Rsv3;
                header = (header << 1) + (int)Rsv4;
                header = (header << 4) + (int)Opcode;
                header = (header << 1) + (int)Mask;
                header = (header << 7) + (int)PayloadLength;
                buff.Write(((ushort)header).InternalToByteArray(ByteOrder.Big), 0, 2);

                if (PayloadLength > 125)
                {
                    buff.Write(ExtendedPayloadLength, 0, PayloadLength == 126 ? 2 : 8);
                }

                if (Mask == Mask.On)
                {
                    buff.Write(MaskingKey, 0, 4);
                }

                if (PayloadLength > 0)
                {
                    var bytes = PayloadData.ToArray();
                    if (PayloadLength < 127)
                    {
                        buff.Write(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        buff.WriteBytes(bytes, 1024);
                    }
                }

                buff.Close();
                var a = buff.ToArray();
                return(a);
            }
        }
Exemplo n.º 2
0
 public string TextToString()
 {
     byte[] bytes = PayloadData.ToArray();
     return(Encoding.UTF8.GetString(bytes));
 }