在C#中发送大于126字节的Websockets [英] Send larger messages than 126 bytes websockets in C#

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

问题描述

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

  public void sendMessage(Stream stream,string message)
{
try
{
List< byte> lb = new List< byte>();
string aux = message;
bool flagStart = false;
int size; (message.Length> _maxLengthMessage)

lb = new List< byte>();
//我把小块中的mesasge剪下来发送
message = aux.Substring(0,_maxLengthMessage);
aux = aux.Substring(_maxLengthMessage);
if(!flagStart)
{
//在Websockets的文档中,我签署了这篇文章:不是结尾,文本
lb.Add(0x01);
flagStart =!flagStart;
}
else
{
//在Websockets的文档中,我签下了这一段:不是结尾,继续
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)
{
//如果这是我们标记的唯一消息:消息结束,文本
lb.Add(0x81);
flagStart =!flagStart;
}
else
{
// else是消息的结尾,但是是连续帧
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;
}

}

我不知道我是否做得很好,但我需要帮助,这是紧急的,我不知道该怎么做,我在互联网上搜索,但我总是找到相同的答案转到The WebSocket协议我做到了,没什么,也许我我不是很了解所以我需要你的帮助。

谢谢。

解决方案

您的代码写入消息长度需要扩展。在协议规范显示缺少的内容。



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

对于大于125但小于等于65536字节的消息,您需要写入3个字节 - 第一个字节是126;以下2个字节给出消息长度。

对于> 65536字节的消息,您需要写入9个字节 - 第一个字节是127;以下8个字节给出消息长度。


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

    }

i don't know if I'm doing very well, but I need help, it's urgent and I don't know what to do, i search on internet but I find always the same answer "Go to the The WebSocket protocol" I did it and nothing, maybe i'm not understanding very well so I need your help.

Thanks.

解决方案

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.

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

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