Skip to content

Basic configuration of WebSocketServer

Psyche edited this page Aug 20, 2024 · 4 revisions

Overview

Here's how to use WebSocketRouteOption

How to configure WebSocket Server:

Fully qualified class name Cyaim.WebSocketServer.Infrastructure.Configures

Introduction to Attribute Configuration

1. ApplicationServices

This property is shared within a single WebSocket Option instance and is primarily obtained internally through DI containers to retrieve key objects. It is automatically set when calling UseWebSocket Server

2. ServerAddresses

This property is generally null, call WebSocketRouteMiddlewareExtensions After the GetWebSocket Address() method, it will be set, usually indicating where the WebSocketServer is listening.

3. ApplicationServiceCollection *

This property is a comparison option, usually configured as the current dependency injection container in the Configuring WebSocket Route () method when configuring the Service (other requirements can be configured themselves). Mainly used to create the class to which the method marked by the hit WebSocket endpoint belongs, using DI container injected constructor parameter objects.

4. InjectionHttpContextPropertyName and InjectionWebSocketClientPropertyName

When the endpoint marked by the WebSocket feature is hit, HttpContext and WebSocket object will be injected into the class properties to which the endpoint belongs, with default property names of WebSocketHttpContext and WebSocketClient, respectively.

You can modify attribute names as needed or remove attributes from the class

5. WebSocketChannels

This property is used to define the WebSocket connection channel. Key represents the connection path, and Value is the entry method of the handler.

WebSocketServer will fully transmit sub protocol information

You can create your own handler to handle WebSocket connections by implementing the IWebSocketHandler interface.

6. WatchAssemblyContext

This attribute stores the basic information read from the assembly pointed to by the WatchAssemblyPath attribute for WebSocket Server to run. If your application includes dynamic code generation, to take immediate effect, you need to update the objects in that property.

7. WatchAssemblyPath

This attribute indicates the assembly path to which the endpoint to be listened to belongs. Default is the executable process in the application domain, or the first executable file executed by the system.

When calling the WebSocketRouteServiceCollectionExtensions.ConfigureWebSocketRoute() method, automatically load and read the data required for WebSocket Server to run and convert it into WatchAssemblyContext object.

8. WatchAssemblyNamespacePrefix

Assembly prefix for Watch [WebSocket],Default:The Controllers folder of this assembly.

Setting this property means that you can freely decide the path containing the class to which the endpoint belongs. For example, in the example, it defaults to Cyanim WebSocketServer.Example.Controllers, You can modify it to Cyaim at will WebSocketServer.Example.WebSocketEndPoints.

9. IsDevelopment

This attribute is true when the value of the environment variable ASPNETCORE_ENVIRONMENT is Development. When this attribute is true, the response body will contain error information.

10. MaxRequestReceiveDataLimit

This attribute represents the upper limit of the volume of a single client request received, counted in bytes. Exceeding the upper limit, this request will end directly.

11. AllowSameConnectionIdAccess

This property indicates whether multiple connections are allowed for one ConnectId (ConnectId refers to the Id obtained from HttpContext.Connection. Id).Default to true.

12. MaxConnectionLimit

This attribute represents the maximum connection limit, which will reject connections if it exceeds the indicated value, but it will not override Kestrel's configuration.

How to configure Kestrel? Please read:https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/servers/kestrel/options?view=aspnetcore-8.0#maximum-client-connections

13. EnableForwardTaskSyncProcessingMode

Indicate whether to perform synchronous processing of requests. If true, the target of each websocket request will wait for processing to complete (ensuring response order). If parallel processing of the target is required for websocket requests, false. Default to false.

14. MaxConnectionParallelForwardLimit

This is at the connection level

Limit the maximum number of tasks forwarded per connection. If it is null, it means unrestricted.

15. MaxEndPointParallelForwardLimit

This is at the endpoint level

Limit the maximum number of tasks forwarded by each endpoint. Key:EndPoint name, Value: SemaphoreSlim Object When waiting for the endpoint task to complete, SemanticSlim will be automatically called WaitAsync().

16. Event

  1. BeforeConnectionEvent
delegate Task<bool> BeforeConnectionHandler(HttpContext context, WebSocketRouteOption webSocketOptions, string channel, ILogger<WebSocketRouteMiddleware> logger)

The prototype of the event delegation is as above. context is the HttpContext object when entering middleware. channel is equivalent to context.Request.Path.

Returning false in the event will prevent the establishment of a connection.

  1. DisconnectedEvent
delegate Task DisconnectedHandler(HttpContext context, WebSocketRouteOption webSocketOptions, string channel, ILogger<WebSocketRouteMiddleware> logger);

Similar to BeforeConnectionEvent.

17. DefaultRequestJsonSerializerOptions

The default Json configuration used by the processing program when handling requests.

new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    WriteIndented = false
}

18. DefaultResponseJsonSerializerOptions

The default Json configuration used by the processing program to handle responses

new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    WriteIndented = false
}
Clone this wiki locally