Table of Contents

Class WebSocketServerFactory

Namespace
Alis.Core.Network
Assembly
Alis.dll

Web socket server factory used to open web socket server connections

public class WebSocketServerFactory : IWebSocketServerFactory

Inheritance

Implements

Inherited Members

Constructors

WebSocketServerFactory()

Initialises a new instance of the WebSocketServerFactory class without caring about internal buffers

public WebSocketServerFactory()

Fields

_bufferFactory

The buffer factory

internal readonly Func<MemoryStream> _bufferFactory

Field Value

Func<MemoryStream>

_bufferPool

The buffer pool

internal readonly IBufferPool _bufferPool

Field Value

IBufferPool

Methods

AcceptWebSocketAsync(WebSocketHttpContext, CancellationToken)

Accept web socket with default options Call ReadHttpHeaderFromStreamAsync first to get WebSocketHttpContext

public Task<WebSocket> AcceptWebSocketAsync(WebSocketHttpContext context, CancellationToken token = default)

Parameters

context WebSocketHttpContext

The http context used to initiate this web socket request

token CancellationToken

The optional cancellation token

Returns

Task<WebSocket>

A connected web socket

AcceptWebSocketAsync(WebSocketHttpContext, WebSocketServerOptions, CancellationToken)

Accept web socket with options specified Call ReadHttpHeaderFromStreamAsync first to get WebSocketHttpContext

public Task<WebSocket> AcceptWebSocketAsync(WebSocketHttpContext context, WebSocketServerOptions options, CancellationToken token = default)

Parameters

context WebSocketHttpContext

The http context used to initiate this web socket request

options WebSocketServerOptions

The web socket options

token CancellationToken

The optional cancellation token

Returns

Task<WebSocket>

A connected web socket

BuildHandshakeResponse(string, string)

Builds the handshake response using the specified sec web socket key

internal static string BuildHandshakeResponse(string secWebSocketKey, string subProtocol)

Parameters

secWebSocketKey string

The sec web socket key

subProtocol string

The sub protocol

Returns

string

The string

CheckWebSocketVersion(string)

Checks the web socket version using the specified http header

internal static void CheckWebSocketVersion(string httpHeader)

Parameters

httpHeader string

The http header

Exceptions

WebSocketVersionNotSupportedException
WebSocketVersionNotSupportedException

Cannot find "Sec-WebSocket-Version" in http header

ExtractWebSocketKey(string)

Extracts the web socket key using the specified http header

internal static string ExtractWebSocketKey(string httpHeader)

Parameters

httpHeader string

The http header

Returns

string

The string

Exceptions

SecWebSocketKeyMissingException

Unable to read "Sec-WebSocket-Key" from http header

ExtractWebSocketVersion(string)

Extracts the web socket version using the specified http header

internal static int ExtractWebSocketVersion(string httpHeader)

Parameters

httpHeader string

The http header

Returns

int

The int

Exceptions

WebSocketVersionNotSupportedException

Cannot find "Sec-WebSocket-Version" in http header

HandleBadRequest(Guid, Exception, Stream, CancellationToken)

Handles the bad request using the specified guid

internal static Task HandleBadRequest(Guid guid, Exception ex, Stream stream, CancellationToken token)

Parameters

guid Guid

The guid

ex Exception

The ex

stream Stream

The stream

token CancellationToken

The token

Returns

Task

HandleWebSocketVersionNotSupported(Guid, WebSocketVersionNotSupportedException, Stream, CancellationToken)

Handles the web socket version not supported using the specified guid

internal static Task HandleWebSocketVersionNotSupported(Guid guid, WebSocketVersionNotSupportedException ex, Stream stream, CancellationToken token)

Parameters

guid Guid

The guid

ex WebSocketVersionNotSupportedException

The ex

stream Stream

The stream

token CancellationToken

The token

Returns

Task

PerformHandshakeAsync(Guid, string, string, Stream, CancellationToken)

Performs the handshake using the specified guid

internal static Task PerformHandshakeAsync(Guid guid, string httpHeader, string subProtocol, Stream stream, CancellationToken token)

Parameters

guid Guid

The guid

httpHeader string

The http header

subProtocol string

The sub protocol

stream Stream

The stream

token CancellationToken

The token

Returns

Task

Exceptions

SecWebSocketKeyMissingException

Unable to read "Sec-WebSocket-Key" from http header

PerformHandshakeWithValidations(Guid, string, string, Stream, CancellationToken)

Performs the handshake with validations using the specified guid

internal static Task PerformHandshakeWithValidations(Guid guid, string httpHeader, string subProtocol, Stream stream, CancellationToken token)

Parameters

guid Guid

The guid

httpHeader string

The http header

subProtocol string

The sub protocol

stream Stream

The stream

token CancellationToken

The token

Returns

Task

ReadHttpHeaderFromStreamAsync(Stream, CancellationToken)

Reads a http header information from a stream and decodes the parts relating to the WebSocket protocot upgrade

public Task<WebSocketHttpContext> ReadHttpHeaderFromStreamAsync(Stream stream, CancellationToken token = default)

Parameters

stream Stream

The network stream

token CancellationToken

The optional cancellation token

Returns

Task<WebSocketHttpContext>

Http data read from the stream

SendHandshakeResponse(Guid, string, Stream, CancellationToken)

Sends the handshake response using the specified guid

internal static Task SendHandshakeResponse(Guid guid, string response, Stream stream, CancellationToken token)

Parameters

guid Guid

The guid

response string

The response

stream Stream

The stream

token CancellationToken

The token

Returns

Task

ValidateWebSocketVersion(int)

Validates the web socket version using the specified sec web socket version

internal static void ValidateWebSocketVersion(int secWebSocketVersion)

Parameters

secWebSocketVersion int

The sec web socket version

Exceptions

WebSocketVersionNotSupportedException