Skip to content

Class WebServerAsync

Namespace: Tool.Sockets.WebHelper
Assembly: Tool.Net.dll

WebServer长连接对象

csharp
public sealed class WebServerAsync : NetworkListener<WebSocketContext>, INetworkListener<WebSocketContext>, INetworkListener, INetworkCore, IDisposable

Inheritance

objectEnServerEventDriveNetworkListener<WebSocketContext>WebServerAsync

Implements

INetworkListener<WebSocketContext>, INetworkListener, INetworkCore, IDisposable

Inherited Members

NetworkListener<WebSocketContext>.ListClient, NetworkListener<WebSocketContext>.Server, NetworkListener<WebSocketContext>.Millisecond, NetworkListener<WebSocketContext>.IsClose, NetworkListener<WebSocketContext>.IsThreadPool, NetworkListener<WebSocketContext>.BufferSize, NetworkListener<WebSocketContext>.CreateSendBytes(WebSocketContext, int), NetworkListener<WebSocketContext>.Dispose(), NetworkListener<WebSocketContext>.OnComplete(in UserKey, EnServer), NetworkListener<WebSocketContext>.SendAsync(SendBytes<WebSocketContext>), NetworkListener<WebSocketContext>.SetCompleted(CompletedEvent<EnServer>), NetworkListener<WebSocketContext>.SetReceived(ReceiveEvent<WebSocketContext>), NetworkListener<WebSocketContext>.StartAsync(string, int), NetworkListener<WebSocketContext>.Stop(), NetworkListener<WebSocketContext>.TrySocket(in UserKey, out WebSocketContext), EnServerEventDrive.OnInterceptor(EnServer, bool), EnServerEventDrive.OnIsQueue(EnServer, bool), EnServerEventDrive.IsEvent(EnServer), EnServerEventDrive.IsQueue(EnServer), EnServerEventDrive.OpenAllEvent(), EnServerEventDrive.OpenAllQueue(), EnServerEventDrive.CloseAllEvent(), EnServerEventDrive.CloseAllQueue(), object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods

ObjectExtension.Add<T>(object, object, object), ObjectExtension.CopyEntity(object, object, params string[]), ObjectExtension.EntityToJson(object), ObjectExtension.EntityToJson(object, bool), ObjectExtension.EntityToJson(object, bool, string), DictionaryExtension.GetDictionary(object), TypeExtension.GetFieldKey(object, string, out bool), TypeExtension.GetFieldKey<T>(object, string, out bool), TypeExtension.GetFieldKey(object, Type, string, out bool), ObjectExtension.GetIntPtr(object), ObjectExtension.GetIntPtrInt(object), TypeExtension.GetPropertieFind(object, string, bool), TypeExtension.GetProperties(object), TypeExtension.GetPropertyKey(object, string, out bool), TypeExtension.GetPropertyKey<T>(object, string, out bool), TypeExtension.GetPropertyKey(object, Type, string, out bool), TypeExtension.GetValue(object, PropertyDescriptor), TypeExtension.GetValue(object, string, bool), TypeExtension.GetValue(object, string), ObjectExtension.Read<T>(object, object, int, int), ObjectExtension.Read<T>(object, int, object, int, int), DictionaryExtension.SetDictionary(object, IDictionary<string, object>), TypeExtension.SetFieldKey(object, string, object), TypeExtension.SetFieldKey<T>(object, string, object), TypeExtension.SetFieldKey(object, Type, string, object), TypeExtension.SetPropertyKey(object, string, object), TypeExtension.SetPropertyKey<T>(object, string, object), TypeExtension.SetPropertyKey(object, Type, string, object), TypeExtension.SetValue(object, PropertyDescriptor, object), TypeExtension.SetValue(object, string, object, bool), TypeExtension.SetValue(object, string, object), ObjectExtension.ToBase64String(object), ObjectExtension.ToBytes(object), ObjectExtension.ToBytes(object, out Type), DictionaryExtension.ToDictionary(object), DictionaryExtension.ToDictionary<T>(object), DictionaryExtension.ToIDictionary(object), DictionaryExtension.ToIDictionary<T>(object), ObjectExtension.ToJson(object), ObjectExtension.ToJson(object, JsonSerializerOptions), ObjectExtension.ToJsonWeb(object), ObjectExtension.ToJsonWeb(object, Action<JsonSerializerOptions>), ObjectExtension.ToTryVar<T>(object, T), ObjectExtension.ToVar<T>(object), ObjectExtension.ToVar(object, Type, bool), ObjectExtension.ToVar(object, string), ObjectExtension.ToXml(object)

Constructors

WebServerAsync()

创建一个 服务器类

csharp
public WebServerAsync()

WebServerAsync(NetBufferSize)

创建一个 服务器类,确认模式和设置流大小

csharp
public WebServerAsync(NetBufferSize size)

Parameters

size NetBufferSize

包大小枚举(警告:请务必保证服务端的大小和客户端一致)

Properties

IsClose

标识服务端连接是否关闭

csharp
public override bool IsClose { get; }

Property Value

bool

IsSSL

获取指示是否使用安全套接字层 (SSL) 保护 WebSocket 连接的值。

csharp
public bool IsSSL { get; init; }

Property Value

bool

Remarks

true 如果使用 SSL; 保护 WebSocket 连接,否则为false。

ListClient

已建立连接的集合 key:UserKey value:WebSocketContext

csharp
public override IReadOnlyDictionary<UserKey, WebSocketContext> ListClient { get; }

Property Value

IReadOnlyDictionary<UserKey, WebSocketContext>

Millisecond

监听控制毫秒

csharp
public override int Millisecond { get; set; }

Property Value

int

Server

服务器创建时的信息

csharp
public override UserKey Server { get; }

Property Value

UserKey

Methods

CreateSendBytes(in UserKey, int)

创建数据发送空间

csharp
public SendBytes<WebSocketContext> CreateSendBytes(in UserKey key, int length)

Parameters

key UserKey

接收者信息

length int

数据大小

Returns

SendBytes<WebSocketContext>

Exceptions

Exception

连接已中断

CreateSendBytes(WebSocketContext, int)

创建数据发送空间

csharp
public override SendBytes<WebSocketContext> CreateSendBytes(WebSocketContext client, int length = 0)

Parameters

client WebSocketContext

收数据的对象

length int

数据大小

Returns

SendBytes<WebSocketContext>

Dispose()

回收资源,并关闭所有连接

csharp
public override void Dispose()

OnComplete(in UserKey, EnServer)

事件方法

csharp
public override ValueTask<IGetQueOnEnum> OnComplete(in UserKey key, EnServer enAction)

Parameters

key UserKey

指定发送对象

enAction EnServer

消息类型

Returns

ValueTask<IGetQueOnEnum>

SendAsync(UserKey, string)

开始异步发送数据

csharp
public ValueTask<bool> SendAsync(UserKey key, string msg)

Parameters

key UserKey

客户端密钥

msg string

要发送的内容

Returns

ValueTask<bool>

SendAsync(WebSocketContext, string)

开始异步发送数据

csharp
public ValueTask<bool> SendAsync(WebSocketContext client, string msg)

Parameters

client WebSocketContext

客户端密钥

msg string

要发送的内容

Returns

ValueTask<bool>

SendAsync(UserKey, Memory<byte>)

开始异步发送数据

csharp
public ValueTask<bool> SendAsync(UserKey key, Memory<byte> Data)

Parameters

key UserKey

客户端密钥

Data Memory<byte>

要发送的内容

Returns

ValueTask<bool>

SendAsync(WebSocketContext, Memory<byte>)

开始异步发送数据

csharp
public ValueTask<bool> SendAsync(WebSocketContext client, Memory<byte> listData)

Parameters

client WebSocketContext

客户端密钥

listData Memory<byte>

要发送的内容

Returns

ValueTask<bool>

SendAsync(SendBytes<WebSocketContext>)

异步发送消息

csharp
public override ValueTask SendAsync(SendBytes<WebSocketContext> sendBytes)

Parameters

sendBytes SendBytes<WebSocketContext>

数据包

Returns

ValueTask

Exceptions

ArgumentException

Client空 或 已断开连接

SetCompleted(CompletedEvent<EnServer>)

连接、发送、关闭事件

csharp
public override void SetCompleted(CompletedEvent<EnServer> Completed)

Parameters

Completed CompletedEvent<EnServer>

SetInitWebContext(Func<HttpListenerContext, Task<HttpListenerWebSocketContext>>)

当新连接,创建时,可以自定义之协议,不实现走默认流程(当实现后,返回null时取消后续业务,请自行关闭连接)

csharp
public void SetInitWebContext(Func<HttpListenerContext, Task<HttpListenerWebSocketContext>> InitWebContext)

Parameters

InitWebContext Func<HttpListenerContext, Task<HttpListenerWebSocketContext>>

SetReceived(ReceiveEvent<WebSocketContext>)

接收到数据事件

csharp
public override void SetReceived(ReceiveEvent<WebSocketContext> Received)

Parameters

Received ReceiveEvent<WebSocketContext>

StartAsync(int)

开始异步监听本机127.0.0.1的端口号

csharp
public Task StartAsync(int port)

Parameters

port int

端口号

Returns

Task

StartAsync(string, int)

开始异步监听ip地址的端口

csharp
public override Task StartAsync(string ip, int port)

Parameters

ip string

可以使用“*”

port int

端口号

Returns

Task

StartAsync(string)

开始异步监听ip地址的端口

csharp
public Task StartAsync(string uriPrefix)

Parameters

uriPrefix string

高级定义法,例如:0.0.0.0:80/tcp,该格式适用

Returns

Task

Stop()

HttpListener关闭

csharp
public override void Stop()

TrySocket(in UserKey, out WebSocketContext)

根据Key获取对应的连接对象

csharp
public override bool TrySocket(in UserKey key, out WebSocketContext client)

Parameters

key UserKey

IP:Port

client WebSocketContext

连接对象

Returns

bool

返回成功状态

基于Apache-2.0协议开源