7

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?

wonea
  • 4,783
  • 17
  • 86
  • 139
udidu
  • 8,269
  • 6
  • 48
  • 68

3 Answers3

7

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.

Community
  • 1
  • 1
dtb
  • 213,145
  • 36
  • 401
  • 431
  • I wrote in my question that I had been there and didn't found any answer, can you be more specific please? – udidu Nov 19 '11 at 13:22
  • [Section 5.4](http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17#section-5.4) – dtb Nov 19 '11 at 13:38
  • Can you help me with that? any pseudo code that will help me receive large size messages will be thankful.. – udidu Nov 21 '11 at 10:14
  • 2
    Seems there is a 2GB limit in some implementations: "So we might want to mention in our docs that we currently support up to 2 GB Web Socket messages, both incoming/outgoing. That's the theoretical limit--malloc is sure to fail on mobile for such sizes, in which case (as spec mandates) the websocket is failed." - https://bugzilla.mozilla.org/show_bug.cgi?id=711003 – Triynko Aug 16 '13 at 00:41
  • I'm not sure the relevance, but this article (linked below) mentions a 125 byte limitation which sounds familiar to the OP's problem. Shouldn't a properly written websocket class already take this into consideration? http://stackoverflow.com/a/9358640 – tresf Dec 07 '15 at 14:52
2

I know for a fact that you can send messages larger than 126 characters,
I am able to send data over protobuf that contains strings that themselves contain 126 characters. http://www.websocket.org/echo.html
If you look at this website you can test out your messages. (Note this does not use fragments)

dtracers
  • 1,534
  • 3
  • 17
  • 37
0

That is not correct what you said DTB. Sending should definitely support more than 126 chars. It is a matter of properly formatting your output. If we were limited to 126 chars there would be no signalling servers for WebRTC. I will code this send message function and post it on here when I finish.