Class WebSocketServerFactory
- Namespace
- Alis.Core.Network
- Assembly
- Alis.dll
Web socket server factory used to open web socket server connections
public class WebSocketServerFactory : IWebSocketServerFactoryInheritance
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> _bufferFactoryField Value
_bufferPool
The buffer pool
internal readonly IBufferPool _bufferPoolField Value
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
contextWebSocketHttpContext-
The http context used to initiate this web socket request
tokenCancellationToken-
The optional cancellation token
Returns
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
contextWebSocketHttpContext-
The http context used to initiate this web socket request
optionsWebSocketServerOptions-
The web socket options
tokenCancellationToken-
The optional cancellation token
Returns
BuildHandshakeResponse(string, string)
Builds the handshake response using the specified sec web socket key
internal static string BuildHandshakeResponse(string secWebSocketKey, string subProtocol)Parameters
Returns
- string
-
The string
CheckWebSocketVersion(string)
Checks the web socket version using the specified http header
internal static void CheckWebSocketVersion(string httpHeader)Parameters
httpHeaderstring-
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
httpHeaderstring-
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
httpHeaderstring-
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
guidGuid-
The guid
exException-
The ex
streamStream-
The stream
tokenCancellationToken-
The token
Returns
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
guidGuid-
The guid
exWebSocketVersionNotSupportedException-
The ex
streamStream-
The stream
tokenCancellationToken-
The token
Returns
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
guidGuid-
The guid
httpHeaderstring-
The http header
subProtocolstring-
The sub protocol
streamStream-
The stream
tokenCancellationToken-
The token
Returns
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
guidGuid-
The guid
httpHeaderstring-
The http header
subProtocolstring-
The sub protocol
streamStream-
The stream
tokenCancellationToken-
The token
Returns
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
streamStream-
The network stream
tokenCancellationToken-
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
guidGuid-
The guid
responsestring-
The response
streamStream-
The stream
tokenCancellationToken-
The token
Returns
ValidateWebSocketVersion(int)
Validates the web socket version using the specified sec web socket version
internal static void ValidateWebSocketVersion(int secWebSocketVersion)Parameters
secWebSocketVersionint-
The sec web socket version