Choosing a buffer size for a WebSocket response Choosing a buffer size for a WebSocket response json json

Choosing a buffer size for a WebSocket response


If I understand the API correctly it will give you the websocket message in multiple parts if necessary.

That means if the message sent from the server is 2048 bytes and you use a 1000 bytes buffer and do:

var buffer = new ArraySegment<byte>(new byte[1000]);var result = await client.ReceiveAsync(buffer, token);

Then in this first call result.Count will be set to 1000 and result.EndOfMessage will be set to false. This means you need to continue reading until EndOfMessage is set to true, which means 3 reads for this example.

If you need everything in one buffer and can't live with processing the message fragments individually you could start with a small buffer and resize the receive buffer if the result tells you that more data is coming in. Thereby you can also check that if a maximum size is exceeded the reception is stopped.

int bufferSize = 1000;var buffer = new byte[bufferSize];var offset = 0;var free = buffer.Length;while (true){    var result = await client.ReceiveAsync(new ArraySegment<byte>(buffer, offset, free), token);    offset += result.Count;    free -= result.Count;    if (result.EndOfMessage) break;    if (free == 0)    {        // No free space        // Resize the outgoing buffer        var newSize = buffer.Length + bufferSize;        // Check if the new size exceeds a limit        // It should suit the data it receives        // This limit however has a max value of 2 billion bytes (2 GB)        if (newSize > maxFrameSize)        {            throw new Exception ("Maximum size exceeded");        }        var newBuffer = new byte[newSize];        Array.Copy(buffer, 0, newBuffer, 0, offset);        buffer = newBuffer;        free = buffer.Length - offset;    }}

And of course you should also check the other fields in the receive result, like MessageType.