发送大于 126 字节 websockets 的消息 [英] Send larger messages than 126 bytes websockets

查看:18
本文介绍了发送大于 126 字节 websockets 的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

    }

有些答案说转到 WebSocket 协议",但它对我不起作用.

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

推荐答案

您编写消息长度的代码需要扩展.协议规范显示了缺失的内容.

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.

对于最多 125 个字节的消息,您的代码是正确的.
对于消息 >125但<= 65536字节,需要写3个字节——第一个字节是126;以下 2 个字节给出了消息长度.
对于消息 >65536字节,需要写9个字节——第一个字节是127;以下 8 个字节给出了消息长度.

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.

这篇关于发送大于 126 字节 websockets 的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆