发送大于 126 字节的 websocket 消息

发布于 2025-01-06 20:20:36 字数 2040 浏览 0 评论 0原文

现在我正在研究Websockets,我是新的,我终于可以发送126字节的消息,但是我需要发送更长的消息,但是当我尝试连接自动关闭时,我的代码是:

    public void sendMessage(Stream stream, string message)
    {
        try
        {
            List<byte> lb = new List<byte>();
            string aux = message;
            bool flagStart = false;
            int size;
            while (message.Length > _maxLengthMessage)
            {
                lb = new List<byte>(); 
                // I cut the mesasge in smaller pieces to send
                message = aux.Substring(0, _maxLengthMessage); 
                aux = aux.Substring(_maxLengthMessage);
                if (!flagStart)
                {
                    // In doc of Websockets i sign this piece: not the end, text
                    lb.Add(0x01);
                    flagStart = !flagStart;
                }
                else
                {
                    // In doc of Websockets i sign this piece: not the end, continuation
                    lb.Add(0x00);
                }
                size = message.Length;

                lb.Add((byte)size);
                lb.AddRange(Encoding.UTF8.GetBytes(message));
                stream.Write(lb.ToArray(), 0, size + 2);             
            }
            lb = new List<byte>();
            if (!flagStart)
            {
                // If is this the only message we mark with: end of message, text
                lb.Add(0x81);
                flagStart = !flagStart;
            }
            else
            {
                //else Is the end of the message but is the continuation frame
                lb.Add(0x80);
            } 
            size = aux.Length;

            lb.Add((byte)size);
            lb.AddRange(Encoding.UTF8.GetBytes(aux));
            //lb.AddRange(Encoding.UTF8.GetBytes(i.ToString()));
            stream.Write(lb.ToArray(), 0, size+2);

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

有些答案说“Go WebSocket 协议”,但它对我不起作用。

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 automatically, my code is:

    public void sendMessage(Stream stream, string message)
    {
        try
        {
            List<byte> lb = new List<byte>();
            string aux = message;
            bool flagStart = false;
            int size;
            while (message.Length > _maxLengthMessage)
            {
                lb = new List<byte>(); 
                // I cut the mesasge in smaller pieces to send
                message = aux.Substring(0, _maxLengthMessage); 
                aux = aux.Substring(_maxLengthMessage);
                if (!flagStart)
                {
                    // In doc of Websockets i sign this piece: not the end, text
                    lb.Add(0x01);
                    flagStart = !flagStart;
                }
                else
                {
                    // In doc of Websockets i sign this piece: not the end, continuation
                    lb.Add(0x00);
                }
                size = message.Length;

                lb.Add((byte)size);
                lb.AddRange(Encoding.UTF8.GetBytes(message));
                stream.Write(lb.ToArray(), 0, size + 2);             
            }
            lb = new List<byte>();
            if (!flagStart)
            {
                // If is this the only message we mark with: end of message, text
                lb.Add(0x81);
                flagStart = !flagStart;
            }
            else
            {
                //else Is the end of the message but is the continuation frame
                lb.Add(0x80);
            } 
            size = aux.Length;

            lb.Add((byte)size);
            lb.AddRange(Encoding.UTF8.GetBytes(aux));
            //lb.AddRange(Encoding.UTF8.GetBytes(i.ToString()));
            stream.Write(lb.ToArray(), 0, size+2);

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

Some answers say "Go to the The WebSocket protocol", but it didn't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2025-01-13 20:20:36

您用于写入消息长度的代码需要扩展。 数据帧图中的扩展负载协议规范显示了缺少的内容。

对于最多 125 字节的消息,您的代码是正确的。

留言> 125但<= 65536字节,需要写3个字节——第一个字节是126;接下来的 2 个字节给出了消息长度。

留言> 65536字节,需要写入9个字节——第一个字节是127;接下来的 8 个字节给出了消息长度。

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.

相对绾红妆 2025-01-13 20:20:36

是的,你必须创建正确的框架,方法如下:

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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文