Send larger messages than 126 bytes websockets

后端 未结 2 835
生来不讨喜
生来不讨喜 2020-12-22 09:07

Now I\'m working on Websockets, I\'m new in that, I finally can send a message of 126 bytes, but I need send longer messages but when I try the connection is closed automati

相关标签:
2条回答
  • 2020-12-22 09:37

    Your code to write the message length needs to be extended. The extended payload in the data framing diagram of the protocol spec shows what's missing.

    For messages up to 125 bytes, your code is correct.
    For messages > 125 but <= 65536 bytes, you need to write 3 bytes - the first byte is 126; the following 2 bytes give the message length.
    For messages > 65536 bytes, you need to write 9 bytes - the first byte is 127; the following 8 bytes give the message length.

    0 讨论(0)
  • 2020-12-22 09:37

    Ye, you have to create correct frame, here is the method:

    static private byte[] CreateFrame(string message, MessageType messageType = MessageType.Text, bool messageContinues = false)
        {
            byte b1 = 0;
            byte b2 = 0;
    
            switch (messageType)
            {
                case MessageType.Continuos:
                    b1 = 0;
                    break;
                case MessageType.Text:
                    b1 = 1;
                    break;
                case MessageType.Binary:
                    b1 = 2;
                    break;
                case MessageType.Close:
                    b1 = 8;
                    break;
                case MessageType.Ping:
                    b1 = 9;
                    break;
                case MessageType.Pong:
                    b1 = 10;
                    break;
            }
    
            b1 = (byte)(b1 + 128); // set FIN bit to 1
    
            byte[] messageBytes = Encoding.UTF8.GetBytes(message);
    
            if (messageBytes.Length < 126)
            {
                b2 = (byte)messageBytes.Length;
            }
            else
            {
                if (messageBytes.Length < Math.Pow(2,16)-1)
                {                   
                    b2 = 126;
    
                }
                else
                {
                    b2 = 127;
                }
    
            }
    
            byte[] frame = null;
    
            if(b2 < 126)
            {
                frame = new byte[messageBytes.Length + 2];
                frame[0] = b1;
                frame[1] = b2;
                Array.Copy(messageBytes, 0, frame, 2, messageBytes.Length);
            }
            if(b2 == 126)
            {
                frame = new byte[messageBytes.Length + 4];
                frame[0] = b1;
                frame[1] = b2;
                byte[] lenght = BitConverter.GetBytes(messageBytes.Length);
                frame[2] = lenght[1];
                frame[3] = lenght[0];
                Array.Copy(messageBytes, 0, frame, 4, messageBytes.Length);
            }
    
            if(b2 == 127)
            {
                frame = new byte[messageBytes.Length + 10];
                frame[0] = b1;
                frame[1] = b2;
                byte[] lenght = BitConverter.GetBytes((long)messageBytes.Length);
    
                for(int i = 7, j = 2; i >= 0; i--, j++)
                {
                    frame[j] = lenght[i];
                }
            }
    
            return frame;
        }
    
    0 讨论(0)
提交回复
热议问题