我怎样才能通过WebSocket发送更大的消息? [英] How can I send larger messages over WebSocket?

查看:341
本文介绍了我怎样才能通过WebSocket发送更大的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#开发一个WebSocket服务器,并且我注意到使用send()方法来自浏览器(在这种情况下为Chrome)的所有消息最多为126个字符长度。
当我想发送大于126个字符的邮件时,总是会发生这种情况,看起来协议会删除大于126个字符的邮件,并只传输前126个字符。
我试图检查协议定义,但没有找到任何答案。

所以,我的问题是,我可以通过WebSockets发送更大的消息吗?

更新:
这是我在C#WebSocket服务器中解析来自客户端(Chrome)的消息的方式:

  private void ReceiveCallback(IAsyncResult _result)
{
lock(lckRead)
{
string message = string .Empty;
int startIndex = 2;
Int64 dataLength =(byte)(buffer [1]& 0x7F); //当消息大时,126个字符在这里切换,我得到的是第一个126个字符
if(dataLength> 0)
{
if(dataLength == 126)
{
BitConverter.ToInt16(buffer,startIndex);
startIndex = 4;
}
else if(dataLength == 127)
{
BitConverter.ToInt64(buffer,startIndex);
startIndex = 10;


bool masked = Convert.ToBoolean((buffer [1]& 0x80)>> 7);
int maskKey = 0;
if(masked)
{
maskKey = BitConverter.ToInt32(buffer,startIndex);
startIndex = startIndex + 4;
}

byte [] payload = new byte [dataLength];
Array.Copy(buffer,(int)startIndex,payload,0,(int)dataLength);
if(masked)
{
payload = MaskBytes(payload,maskKey);
message = Encoding.UTF8.GetString(payload);
OnDataReceived(new DataReceivedEventArgs(message.Length,message));
}

HandleMessage(message); //'message' - 收到

的消息Listen();
}
else
{
if(ClientDisconnected!= null)
ClientDisconnected(this,EventArgs.Empty);
}
}
}

我还是不明白我怎么能得到更大的消息,它可能与操作码有关,但我不知道要改变它以使其工作? WebSocket消息可以是任何大小的。然而,大消息通常以多个部分(片段)传输,以避免线头阻塞。详情请参阅 WebSockets I-D


I am developing a WebSocket server with C# and I noticed that all the messages that coming from the browser (Chrome in that case) using the send() method are 126 chars length max. It happens all the time when I want to send messages larger that 126 chars, looks like the protocol cuts any message larger then 126 chars and transfer only the first 126 chars. I tried to check on the protocol definition but didn't find any answer.

So, my question is, can I send larger messages over WebSockets?

UPDATE: This is the way that I'm parsing the messages from the client (Chrome) in my C# WebSocket server:

    private void ReceiveCallback(IAsyncResult _result)
    {
        lock (lckRead)
        {
            string message = string.Empty;
            int startIndex = 2;
            Int64 dataLength = (byte)(buffer[1] & 0x7F); // when the message is larger then 126 chars it cuts here and all i get is the first 126 chars
            if (dataLength > 0)
            {
                if (dataLength == 126)
                {
                    BitConverter.ToInt16(buffer, startIndex);
                    startIndex = 4;
                }
                else if (dataLength == 127)
                {
                    BitConverter.ToInt64(buffer, startIndex);
                    startIndex = 10;
                }

                bool masked = Convert.ToBoolean((buffer[1] & 0x80) >> 7);
                int maskKey = 0;
                if (masked)
                {
                    maskKey = BitConverter.ToInt32(buffer, startIndex);
                    startIndex = startIndex + 4;
                }

                byte[] payload = new byte[dataLength];
                Array.Copy(buffer, (int)startIndex, payload, 0, (int)dataLength);
                if (masked)
                {
                    payload = MaskBytes(payload, maskKey);
                    message = Encoding.UTF8.GetString(payload);
                    OnDataReceived(new DataReceivedEventArgs(message.Length, message));
                }

                HandleMessage(message); //'message' -  the message that received

                Listen();
            }
            else
            {
                if (ClientDisconnected != null)
                    ClientDisconnected(this, EventArgs.Empty);
            }
        }
    }

I still didn't understand how can I get larger message, its maybe something with the opcode, but I don't know what to change to make it work?

解决方案

WebSocket messages can be of any size. However, large messages are usually transmitted in multiple parts (fragments) to avoid head-of-line blocking. See the WebSockets I-D for details.

这篇关于我怎样才能通过WebSocket发送更大的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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