Matt
Matt

Reputation: 3677

WebSocket.CreateServerBuffer vs. WebSocket.CreateClientBuffer

I've been able to implement a server-side Web Socket solution using ASP.NET Core, however, I can't find any good documentation on the difference between WebSocket.CreateServerBuffer and WebSocket.CreateClientBuffer, and when to use either.

Can anyone shed some light on when/how to use them?

Upvotes: 2

Views: 2305

Answers (1)

Svek
Svek

Reputation: 12898

Usage

WebSocket.CreateClientBuffer is primarily called with the ConnectAsync() method an instance of ClientWebSocket.

ClientWebSocket clientWS = new ClientWebSocket();
await clientWS.ConnectAsync(...);  // will create the client buffer

CreateClientBuffer vs CreateServerBuffer

The only difference, essentially is that CreateClientBuffer allows you to define the sendBufferSize, whereas the CreateServerBuffer will not (and will default the sendBufferSize to 16).

It appears that they both exist mainly to serve the purpose of separating the concerns between a ClientWebSocket and a ServerWebSocket which are both just instances derived from the abstract class WebSocket.


Additional Detail

Take look at the WebSocket.cs at Microsoft Reference Source to see the implementation:

// [WebSocketBuffer.cs] const int MinSendBufferSize = 16;

public static ArraySegment<byte> CreateClientBuffer(int receiveBufferSize, int sendBufferSize)
{
    WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, sendBufferSize);

    return WebSocketBuffer.CreateInternalBufferArraySegment(receiveBufferSize, sendBufferSize, false);
}

public static ArraySegment<byte> CreateServerBuffer(int receiveBufferSize)
{
    WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, WebSocketBuffer.MinSendBufferSize);

    return WebSocketBuffer.CreateInternalBufferArraySegment(receiveBufferSize, WebSocketBuffer.MinSendBufferSize, true);
}

They both create a new instance of a WebSocketBuffer but pass different minimum sendBufferSize values

The CreateServerBuffer() method will always use the MinSendBufferSize = 16. Otherwise, the CreateClientBuffer() uses the value passed.

Upvotes: 4

Related Questions