Table of Contents

Class WebSocketClientFactory

Namespace
Alis.Core.Network
Assembly
Alis.dll

Web socket client factory used to open web socket client connections

public class WebSocketClientFactory : IWebSocketClientFactory, IDisposable

Inheritance

Implements

Inherited Members

Constructors

WebSocketClientFactory()

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

public WebSocketClientFactory()

WebSocketClientFactory(Func<MemoryStream>)

Initialises a new instance of the WebSocketClientFactory class with control over internal buffer creation

public WebSocketClientFactory(Func<MemoryStream> bufferFactory)

Parameters

bufferFactory Func<MemoryStream>

Used to get a memory stream. Feel free to implement your own buffer pool. MemoryStreams will be disposed when no longer needed and can be returned to the pool.

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

tcpClient

The tcp client

internal TcpClient tcpClient

Field Value

TcpClient

Methods

BuildHandshakeRequest(Uri, string, string, string)

Builds the handshake request using the specified uri

internal string BuildHandshakeRequest(Uri uri, string secWebSocketKey, string secWebSocketProtocol, string additionalHeaders)

Parameters

uri Uri

The uri

secWebSocketKey string

The sec web socket key

secWebSocketProtocol string

The sec web socket protocol

additionalHeaders string

The additional headers

Returns

string

The string

ConnectAsync(Uri, CancellationToken)

Connect with default options

public Task<WebSocket> ConnectAsync(Uri uri, CancellationToken token = default)

Parameters

uri Uri

The WebSocket uri to connect to (e.g. ws://example.com or wss://example.com for SSL)

token CancellationToken

The optional cancellation token

Returns

Task<WebSocket>

A connected web socket instance

ConnectAsync(Uri, WebSocketClientOptions, CancellationToken)

Connect with options specified

public Task<WebSocket> ConnectAsync(Uri uri, WebSocketClientOptions options, CancellationToken token = default)

Parameters

uri Uri

The WebSocket uri to connect to (e.g. ws://example.com or wss://example.com for SSL)

options WebSocketClientOptions

The WebSocket client options

token CancellationToken

The optional cancellation token

Returns

Task<WebSocket>

A connected web socket instance

ConnectAsync(Stream, string, WebSocketClientOptions, CancellationToken)

Connect with a stream that has already been opened and HTTP websocket upgrade request sent This function will check the handshake response from the server and proceed if successful Use this function if you have specific requirements to open a conenction like using special http headers and cookies You will have to build your own HTTP websocket upgrade request You may not even choose to use TCP/IP and this function will allow you to do that

public Task<WebSocket> ConnectAsync(Stream responseStream, string secWebSocketKey, WebSocketClientOptions options, CancellationToken token = default)

Parameters

responseStream Stream

The full duplex response stream from the server

secWebSocketKey string

The secWebSocketKey you used in the handshake request

options WebSocketClientOptions

The WebSocket client options

token CancellationToken

The optional cancellation token

Returns

Task<WebSocket>

ConnectAsync(Guid, Stream, string, TimeSpan, string, bool, CancellationToken)

Connects the guid

internal Task<WebSocket> ConnectAsync(Guid guid, Stream responseStream, string secWebSocketKey, TimeSpan keepAliveInterval, string secWebSocketExtensions, bool includeExceptionInCloseResponse, CancellationToken token)

Parameters

guid Guid

The guid

responseStream Stream

The response stream

secWebSocketKey string

The sec web socket key

keepAliveInterval TimeSpan

The keep alive interval

secWebSocketExtensions string

The sec web socket extensions

includeExceptionInCloseResponse bool

The include exception in close response

token CancellationToken

The token

Returns

Task<WebSocket>

A task containing the web socket

Exceptions

WebSocketHandshakeFailedException

Handshake unexpected failure

Dispose()

Disposes this instance

public void Dispose()

GenerateSecWebSocketKey()

Generates the sec web socket key

internal string GenerateSecWebSocketKey()

Returns

string

The string

GetAdditionalHeaders(Dictionary<string, string>)

Gets the additional headers using the specified additional headers

internal static string GetAdditionalHeaders(Dictionary<string, string> additionalHeaders)

Parameters

additionalHeaders Dictionary<string, string>

The additional headers

Returns

string

The string

GetStream(Guid, bool, bool, string, int, CancellationToken)

Override this if you need more control over how the stream used for the websocket is created. It does not event need to be a TCP stream

internal virtual Task<Stream> GetStream(Guid loggingGuid, bool isSecure, bool noDelay, string host, int port, CancellationToken cancellationToken)

Parameters

loggingGuid Guid

For logging purposes only

isSecure bool

Make a secure connection

noDelay bool

Set to true to send a message immediately with the least amount of latency (typical usage for chat)

host string

The destination host (can be an IP address)

port int

The destination port

cancellationToken CancellationToken

Used to cancel the request

Returns

Task<Stream>

A connected and open stream

GetSubProtocolFromHeader(string)

Gets the sub protocol from header using the specified response

internal string GetSubProtocolFromHeader(string response)

Parameters

response string

The response

Returns

string

The string

PerformHandshake(Guid, Uri, Stream, WebSocketClientOptions, CancellationToken)

Performs the handshake using the specified guid

internal Task<WebSocket> PerformHandshake(Guid guid, Uri uri, Stream stream, WebSocketClientOptions options, CancellationToken token)

Parameters

guid Guid

The guid

uri Uri

The uri

stream Stream

The stream

options WebSocketClientOptions

The options

token CancellationToken

The token

Returns

Task<WebSocket>

A task containing the web socket

SendHandshakeRequest(Stream, string, Guid)

Sends the handshake request using the specified stream

internal Task SendHandshakeRequest(Stream stream, string handshakeHttpRequest, Guid guid)

Parameters

stream Stream

The stream

handshakeHttpRequest string

The handshake http request

guid Guid

The guid

Returns

Task

ThrowIfInvalidAcceptString(Guid, string, string)

Throws the if invalid accept string using the specified guid

internal void ThrowIfInvalidAcceptString(Guid guid, string response, string secWebSocketKey)

Parameters

guid Guid

The guid

response string

The response

secWebSocketKey string

The sec web socket key

Exceptions

WebSocketHandshakeFailedException

ThrowIfInvalidResponseCode(string)

Throws the if invalid response code using the specified response header

internal void ThrowIfInvalidResponseCode(string responseHeader)

Parameters

responseHeader string

The response header

Exceptions

InvalidHttpResponseCodeException
InvalidHttpResponseCodeException

null null

TlsAuthenticateAsClient(SslStream, string)

Override this if you need more fine grained control over the TLS handshake like setting the SslProtocol or adding a client certificate

internal virtual void TlsAuthenticateAsClient(SslStream sslStream, string host)

Parameters

sslStream SslStream
host string

ValidateServerCertificate(object, X509Certificate, X509Chain, SslPolicyErrors)

Invoked by the RemoteCertificateValidationDelegate If you want to ignore certificate errors (for debugging) then return true

internal static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

Parameters

sender object
certificate X509Certificate
chain X509Chain
sslPolicyErrors SslPolicyErrors

Returns

bool