简体   繁体   中英

JS WebSocket client with C# WebSocket Server error 1006

I'm trying to send some data from server written in C# to browser client written in JS. When I send 12900 bytes sized messages then all going OK. If I'll send message sized 67990 bytes then on server-side I'll get no error and client side I'll get disconnect with disconnect error 1006 with no other explanation. I can't find somewhere any limitations related to message size. Here is C# code in which I'm trying to send data.

byte[] data =getDecodedMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dataResponse)));
stream.Write(data, 0, data.Length);

Based on question user3892585 answer I replaced bytesRaw.Length with bytesRaw.LongLength in my encoding function for WebSocket data transfer. After doing this I didn't receive socket error.

public static byte[] getEncodedMessage(byte[] bytesRaw)
    {
        List<byte> bytesFormatted = new List<byte>();
        bytesFormatted.Add(129);

        int indexStartRawData = -1; // it doesn't matter what value is
                                    // set here - it will be set now:

        if (bytesRaw.LongLength <= 125)
        {
            bytesFormatted.Add(Convert.ToByte(bytesRaw.LongLength));


            indexStartRawData = 2;
        }

        else if (bytesRaw.LongLength >= 126 && bytesRaw.LongLength <= 65535)
        {
            bytesFormatted.Add(126);
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 8) & 255));
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength) & 255));

            indexStartRawData = 4;
        }

        else
        {
            bytesFormatted.Add(127);
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 56) & 255));
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 48) & 255));


            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 40) & 255));
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 32) & 255));
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 24) & 255));
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 16) & 255));
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength >> 8) & 255));
            bytesFormatted.Add(Convert.ToByte((bytesRaw.LongLength) & 255));

            indexStartRawData = 10;
        }
        // put raw data at the correct index
        bytesFormatted.InsertRange(indexStartRawData, bytesRaw);
        return bytesFormatted.ToArray();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM