Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket is not able to send large data

How can I send Large data to WebSocket in Java Script?

Using below code I am able to send data for 126 characters but not more then this. (Written in C#)

public static void SendData(string text)
    {

        foreach (SocketClient client in ClientList)
        {
            if (client.Client.Connected)
            {
                try
                {

                    NetworkStream l_Stream = client.Client.GetStream();

                    List<byte> lb = new List<byte>();
                    lb = new List<byte>();
                    lb.Add(0x81);
                    int size = text.Length;
                    lb.Add((byte)size);
                    lb.AddRange(Encoding.UTF8.GetBytes(text));
                    l_Stream.Write(lb.ToArray(), 0, size + 2);

                }
                catch
                {
                    CloseClient(client);
                }
            }
        }
    }
}

Can someone please help me? I tried to use so many things but none of them are working for me.

I am using Chrome 25 for the same.

like image 402
Sanket Shah Avatar asked Dec 31 '12 14:12

Sanket Shah


1 Answers

This is how a websocket frame looks according to RFC 6455:

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-------+-+-------------+-------------------------------+
 |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
 |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
 |N|V|V|V|       |S|             |   (if payload len==126/127)   |
 | |1|2|3|       |K|             |                               |
 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
 |     Extended payload length continued, if payload len == 127  |
 + - - - - - - - - - - - - - - - +-------------------------------+
 |                               |Masking-key, if MASK set to 1  |
 +-------------------------------+-------------------------------+
 | Masking-key (continued)       |          Payload Data         |
 +-------------------------------- - - - - - - - - - - - - - - - +
 :                     Payload Data continued ...                :
 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
 |                     Payload Data continued ...                |
 +---------------------------------------------------------------+

You are setting the payload length of the websocket frame in byte 1 (the 2nd byte - first byte is byte 0). A byte can only have 256 states. But the first bit of byte 1 is used for the masking flag. So you can only represent values from 0 to 127.

When the payload length is larger than 125, you have to set byte 1 to 126 and put the length into byte 2 and 3 ("Extended payload length"). When your payload is even longer than 65535 bytes, you have to set byte 1 to 127 and put the payload length into the bytes 2-9 ("Extended payload length" and "Extended payload length continued"). When your payload length is even larger than 64bit (16 Exabyte, or about 16 million Terabyte), then... you should rather send a bunch of trucks filled with hard drives.

More details about that can be found in the official Websocket RFC.

like image 112
Philipp Avatar answered Sep 28 '22 13:09

Philipp