如何通过WebSocket发送更大的消息?

udi*_*idu 7 .net c# google-chrome 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); // 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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我仍然不明白我怎么能得到更大的消息,它可能与操作码有关,但我不知道要改变什么才能使它工作?

dtb*_*dtb 7

WebSocket消息可以是任何大小.但是,大消息通常以多个部分(片段)传输,以避免线头阻塞.有关详细信息,请参阅WebSockets ID.

  • 在某些实现中似乎有2GB的限制:"所以我们可能想在我们的文档中提到我们目前支持最多2 GB Web Socket消息,包括传入/传出.这是理论上的限制 - malloc肯定会在移动设备上失败对于这样的尺寸,在这种情况下(如规格要求),websocket失败." - https://bugzilla.mozilla.org/show_bug.cgi?id=711003 (2认同)