mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
从Serein.Library分离了WebSocket/Modbus;新增了Json门户类,用于未来的Http、WebSocket、Mqtt、gRPC、QUIC扩展。
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
namespace Serein.Proto.WebSocket.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>作用:WebSocket中处理Json时,将通过Json中ThemeKey 对应的内容(ThemeValue)自动路由到相应方法进行处理,同时要求Data中必须存在对应入参。</para>
|
||||
/// <para>如果没有显式设置 ThemeValue,将默认使用方法名称作为ThemeValue。</para>
|
||||
/// <para>如果没有显式设置 IsReturnValue 标记为 false ,当方法顺利完成(没有抛出异常,且返回对象非null),会自动转为json文本发送回去</para>
|
||||
/// <para>如果没有显式设置 ArgNotNull 标记为 false ,当外部尝试调用时,若 Json Data 不包含响应的数据,将会被忽略此次调用</para>
|
||||
/// <para>如果返回类型为Task或Task<TResult>,将会自动等待异步完成并获取结果(无法处理Task<Task<TResult>>的情况)。</para>
|
||||
/// <para>如果返回了值类型,会自动装箱为引用对象。</para>
|
||||
/// <para>如果有方法执行过程中发送消息的需求,请在入参中声明以下类型的成员,调用时将传入发送消息的委托。</para>
|
||||
/// <para>Action<string> : 发送文本内容。</para>
|
||||
/// <para>Action<object> : 会自动将对象解析为Json字符串,发送文本内容。</para>
|
||||
/// <para>Action<dynamic> : 会自动将对象解析为Json字符串,发送文本内容。</para>
|
||||
/// <para>Func<string,Task> : 异步发送文本内容。</para>
|
||||
/// <para>Func<object,Task> : 会自动将对象解析为Json字符串,异步发送文本内容。</para>
|
||||
/// <para>Func<dynamic,Task> : 会自动将对象解析为Json字符串,异步发送文本内容。</para>
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class AutoSocketHandleAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 描述Json业务字段,如果不设置,将默认使用方法名称。
|
||||
/// </summary>
|
||||
public string ThemeValue = string.Empty;
|
||||
/// <summary>
|
||||
/// <para>标记方法执行完成后是否需要将结果发送。</para>
|
||||
/// <para>注意以下返回值,返回的 json 中将不会新建 DataKey 字段:</para>
|
||||
/// <para>1.返回类型为 void </para>
|
||||
/// <para>2.返回类型为 Task </para>
|
||||
/// <para>2.返回类型为 Unit </para>
|
||||
/// <para>补充:如果返回类型是Task<TResult></para>
|
||||
/// <para>会进行异步等待,当Task结束后,自动获取TResult进行发送(请避免Task<Task<TResult>>诸如此类的Task泛型嵌套)</para>
|
||||
/// </summary>
|
||||
public bool IsReturnValue = true;
|
||||
/// <summary>
|
||||
/// <para>表示该方法所有入参不能为空(所需的参数在请求Json的Data不存在)</para>
|
||||
/// <para>若有一个参数无法从data获取,则不会进行调用该方法</para>
|
||||
/// <para>如果设置该属性为 false ,但某些入参不能为空,而不希望在代码中进行检查,请为入参添加[NotNull]/[Needful]特性</para>
|
||||
/// </summary>
|
||||
public bool ArgNotNull = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
namespace Serein.Proto.WebSocket.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>标记该类是处理模板,需要获取WebSocketServer/WebSocketClient了实例后,使用(Server/Client).MsgHandleHelper.AddModule()进行添加。</para>
|
||||
/// <para>处理模板需要继承 ISocketHandleModule 接口,否则WebSocket接受到数据时,将无法进行调用相应的处理模板。</para>
|
||||
/// <para>使用方式:</para>
|
||||
/// <para>[AutoSocketModule(ThemeKey = "theme", DataKey = "data")]</para>
|
||||
/// <para>public class PlcSocketService : ISocketHandleModule</para>
|
||||
/// <para>类中方法示例:void AddUser(string name,int age)</para>
|
||||
/// <para>Json示例:{ "theme":"AddUser", //【ThemeKey】 </para>
|
||||
/// <para> "data": { // 【DataKey】 </para>
|
||||
/// <para> "name":"张三", </para>
|
||||
/// <para> "age":35, } } </para>
|
||||
/// <para>WebSocket中收到以上该Json时,通过ThemeKey获取到"AddUser",然后找到AddUser()方法</para>
|
||||
/// <para>然后根据方法入参名称,从data对应的json数据中取出"name""age"对应的数据作为入参进行调用。AddUser("张三",35)</para>
|
||||
/// <para></para>
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public sealed class AutoSocketModuleAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 业务标识
|
||||
/// </summary>
|
||||
public string ThemeKey;
|
||||
/// <summary>
|
||||
/// 数据标识
|
||||
/// </summary>
|
||||
public string DataKey;
|
||||
/// <summary>
|
||||
/// ID标识
|
||||
/// </summary>
|
||||
public string MsgIdKey;
|
||||
|
||||
/// <summary>
|
||||
/// 指示应答数据回复方法返回值
|
||||
/// </summary>
|
||||
public bool IsResponseUseReturn = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
23
Serein.Proto.WebSocket/Attributes/UseDataAttribute.cs
Normal file
23
Serein.Proto.WebSocket/Attributes/UseDataAttribute.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Net.WebSockets;
|
||||
using Serein.Library;
|
||||
|
||||
|
||||
|
||||
namespace Serein.Proto.WebSocket.Attributes
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 使用 DataKey 整体数据
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Parameter)]
|
||||
public sealed class UseDataAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
14
Serein.Proto.WebSocket/Attributes/UseMsgIdAttribute.cs
Normal file
14
Serein.Proto.WebSocket/Attributes/UseMsgIdAttribute.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Serein.Proto.WebSocket.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用 MsgIdKey 整体数据
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Parameter)]
|
||||
public sealed class UseMsgIdAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
12
Serein.Proto.WebSocket/Attributes/UseRequestAttribute.cs
Normal file
12
Serein.Proto.WebSocket/Attributes/UseRequestAttribute.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Proto.WebSocket.Attributes
|
||||
{
|
||||
internal sealed class UseRequestAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
14
Serein.Proto.WebSocket/Handle/Attribute.cs
Normal file
14
Serein.Proto.WebSocket/Handle/Attribute.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace Serein.Proto.WebSocket.Handle
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示参数不能为空(Net462不能使用NutNull的情况)
|
||||
/// </summary>
|
||||
public sealed class NeedfulAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
73
Serein.Proto.WebSocket/Handle/HandleConfiguration.cs
Normal file
73
Serein.Proto.WebSocket/Handle/HandleConfiguration.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using Serein.Library;
|
||||
|
||||
|
||||
|
||||
namespace Serein.Proto.WebSocket.Handle
|
||||
{
|
||||
/// <summary>
|
||||
/// socket模块处理数据配置
|
||||
/// </summary>
|
||||
|
||||
public class HandleConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Emit委托
|
||||
/// </summary>
|
||||
public DelegateDetails DelegateDetails { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 未捕获的异常跟踪
|
||||
/// </summary>
|
||||
public Action<Exception, Action<object>> OnExceptionTracking { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所使用的实例
|
||||
/// </summary>
|
||||
public Func<ISocketHandleModule> InstanceFactory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否需要返回
|
||||
/// </summary>
|
||||
public bool IsReturnValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否要求必须不为null
|
||||
/// </summary>
|
||||
public bool ArgNotNull { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用Data整体内容作为入参参数
|
||||
/// </summary>
|
||||
public bool[] UseData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用Request整体内容作为入参参数
|
||||
/// </summary>
|
||||
public bool[] UseRequest { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用消息ID作为入参参数
|
||||
/// </summary>
|
||||
public bool[] UseMsgId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 参数名称
|
||||
/// </summary>
|
||||
public string[] ParameterName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 参数类型
|
||||
/// </summary>
|
||||
public Type[] ParameterType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否检查变量为空
|
||||
/// </summary>
|
||||
public bool[] IsCheckArgNotNull { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Serein.Proto.WebSocket.Handle
|
||||
{
|
||||
internal class WebSocketHandleConfiguration : HandleConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// 主题
|
||||
/// </summary>
|
||||
public string ThemeValue { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
270
Serein.Proto.WebSocket/Handle/WebSocketHandleModule.cs
Normal file
270
Serein.Proto.WebSocket/Handle/WebSocketHandleModule.cs
Normal file
@@ -0,0 +1,270 @@
|
||||
using Serein.Library;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Proto.WebSocket.Handle
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Json消息处理模块
|
||||
/// </summary>
|
||||
public class WebSocketHandleModule
|
||||
{
|
||||
/// <summary>
|
||||
/// Json消息处理模块
|
||||
/// </summary>
|
||||
public WebSocketHandleModule(WebSocketHandleModuleConfig config)
|
||||
{
|
||||
moduleConfig = config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模块的处理配置
|
||||
/// </summary>
|
||||
private readonly WebSocketHandleModuleConfig moduleConfig;
|
||||
|
||||
/// <summary>
|
||||
/// 用来判断消息是否重复
|
||||
/// </summary>
|
||||
private HashSet<string> _myMsgIdHash = new HashSet<string>();
|
||||
/// <summary>
|
||||
/// 存储处理数据的配置
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<string, HandleConfiguration> MyHandleConfigs = new ConcurrentDictionary<string, HandleConfiguration>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加处理配置
|
||||
/// </summary>
|
||||
/// <param name="config">处理模块</param>
|
||||
internal bool AddHandleConfigs(WebSocketHandleConfiguration config)
|
||||
{
|
||||
if (!MyHandleConfigs.ContainsKey(config.ThemeValue))
|
||||
{
|
||||
MyHandleConfigs[config.ThemeValue] = config;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除某个处理模块
|
||||
/// </summary>
|
||||
/// <param name="socketControlBase"></param>
|
||||
/// <returns></returns>
|
||||
public bool RemoveConfig(ISocketHandleModule socketControlBase)
|
||||
{
|
||||
foreach (var kv in MyHandleConfigs.ToArray())
|
||||
{
|
||||
var config = kv.Value;
|
||||
MyHandleConfigs.TryRemove(kv.Key, out _);
|
||||
|
||||
}
|
||||
return MyHandleConfigs.Count == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 卸载当前模块的所有配置
|
||||
/// </summary>
|
||||
public void UnloadConfig()
|
||||
{
|
||||
var temp = MyHandleConfigs.Values;
|
||||
MyHandleConfigs.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 处理JSON数据
|
||||
/// </summary>
|
||||
public async Task HandleAsync(WebSocketMsgContext context)
|
||||
{
|
||||
var jsonObject = context.MsgRequest; // 获取到消息
|
||||
string theme = jsonObject.GetValue(moduleConfig.ThemeJsonKey)?.ToString();
|
||||
if (!MyHandleConfigs.TryGetValue(theme, out var handldConfig))
|
||||
{
|
||||
return; // 没有主题
|
||||
}
|
||||
context.MsgTheme = theme; // 添加主题
|
||||
string msgId = jsonObject.GetValue(moduleConfig.MsgIdJsonKey)?.ToString();
|
||||
if (_myMsgIdHash.Contains(msgId))
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 消息重复");
|
||||
return;
|
||||
}
|
||||
context.MsgId = msgId; // 添加 ID
|
||||
_myMsgIdHash.Add(msgId);
|
||||
|
||||
try
|
||||
{
|
||||
var dataObj = jsonObject.GetValue(moduleConfig.DataJsonKey);
|
||||
context.MsgData = dataObj; // 添加消息
|
||||
if (TryGetParameters(handldConfig, context, out var args))
|
||||
{
|
||||
var result = await HandleAsync(handldConfig, args);
|
||||
if (handldConfig.IsReturnValue)
|
||||
{
|
||||
await context.RepliedAsync(moduleConfig, context, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.ERROR, $"error in ws : {ex.Message}{Environment.NewLine}json value:{jsonObject}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
context.Handle = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 调用
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<object> HandleAsync(HandleConfiguration config, object[] args)
|
||||
{
|
||||
var instance = config.InstanceFactory.Invoke();
|
||||
var result = await config.DelegateDetails.InvokeAsync(instance, args);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取入参参数
|
||||
/// </summary>
|
||||
/// <param name="config">处理配置</param>
|
||||
/// <param name="context">处理上下文</param>
|
||||
/// <param name="args">返回的入参参数</param>
|
||||
/// <returns></returns>
|
||||
internal static bool TryGetParameters(HandleConfiguration config, WebSocketMsgContext context, out object[] args)
|
||||
{
|
||||
args = new object[config.ParameterType.Length];
|
||||
bool isCanInvoke = true; ; // 表示是否可以调用方法
|
||||
|
||||
for (int i = 0; i < config.ParameterType.Length; i++)
|
||||
{
|
||||
|
||||
var type = config.ParameterType[i]; // 入参变量类型
|
||||
var argName = config.ParameterName[i]; // 入参参数名称
|
||||
#region 传递消息ID
|
||||
if (config.UseMsgId[i])
|
||||
{
|
||||
args[i] = context.MsgId;
|
||||
}
|
||||
#endregion
|
||||
#region DATA JSON数据
|
||||
else if (config.UseRequest[i])
|
||||
{
|
||||
args[i] = context.MsgRequest.ToObject(type);
|
||||
}
|
||||
#endregion
|
||||
#region DATA JSON数据
|
||||
else if (config.UseData[i])
|
||||
{
|
||||
args[i] = context.MsgData.ToObject(type);
|
||||
}
|
||||
#endregion
|
||||
#region 值类型参数
|
||||
else if (type.IsValueType)
|
||||
{
|
||||
var jsonValue = context.MsgData.GetValue(argName);
|
||||
if (!(jsonValue is null))
|
||||
{
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (config.ArgNotNull && !config.IsCheckArgNotNull[i]) // 检查不能为空
|
||||
{
|
||||
|
||||
args[i] = Activator.CreateInstance(type); // 值类型返回默认值
|
||||
}
|
||||
else
|
||||
{
|
||||
isCanInvoke = false; // 参数不能为空,终止调用
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region 引用类型参数
|
||||
else if (type.IsClass)
|
||||
{
|
||||
var jsonValue = context.MsgData.GetValue(argName);
|
||||
if (!(jsonValue is null))
|
||||
{
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (config.ArgNotNull && !config.IsCheckArgNotNull[i])
|
||||
{
|
||||
|
||||
args[i] = null; // 引用类型返回null
|
||||
}
|
||||
else
|
||||
{
|
||||
isCanInvoke = false; // 参数不能为空,终止调用
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region 传递消息委托
|
||||
else if (type.IsGenericType) // 传递SendAsync委托
|
||||
{
|
||||
if (type.IsAssignableFrom(typeof(Func<object, Task>)))
|
||||
{
|
||||
args[i] = new Func<object, Task>(async data =>
|
||||
{
|
||||
var jsonText = JsonHelper.Serialize(data);
|
||||
await context.SendAsync(jsonText);
|
||||
});
|
||||
}
|
||||
else if (type.IsAssignableFrom(typeof(Func<string, Task>)))
|
||||
{
|
||||
args[i] = new Func<string, Task>(async data =>
|
||||
{
|
||||
await context.SendAsync(data);
|
||||
});
|
||||
}
|
||||
else if (type.IsAssignableFrom(typeof(Action<object>)))
|
||||
{
|
||||
args[i] = new Action<object>(async data =>
|
||||
{
|
||||
var jsonText = JsonHelper.Serialize(data);
|
||||
await context.SendAsync(jsonText);
|
||||
});
|
||||
}
|
||||
else if (type.IsAssignableFrom(typeof(Action<string>)))
|
||||
{
|
||||
args[i] = new Action<string>(async data =>
|
||||
{
|
||||
var jsonText = JsonHelper.Serialize(data);
|
||||
await context.SendAsync(jsonText);
|
||||
});
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
return isCanInvoke;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
26
Serein.Proto.WebSocket/Handle/WebSocketHandleModuleConfig.cs
Normal file
26
Serein.Proto.WebSocket/Handle/WebSocketHandleModuleConfig.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace Serein.Proto.WebSocket.Handle
|
||||
{
|
||||
/// <summary>
|
||||
/// 远程环境配置
|
||||
/// </summary>
|
||||
public class WebSocketHandleModuleConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 有关消息ID的 Json Key
|
||||
/// </summary>
|
||||
public string MsgIdJsonKey { get; set; }
|
||||
/// <summary>
|
||||
/// 有关消息主题的 Json Key
|
||||
/// </summary>
|
||||
public string ThemeJsonKey { get; set; }
|
||||
/// <summary>
|
||||
/// 有关数据的 Json Key
|
||||
/// </summary>
|
||||
public string DataJsonKey { get; set; }
|
||||
/// <summary>
|
||||
/// 使用怎么样的数据
|
||||
/// </summary>
|
||||
public bool IsResponseUseReturn { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
115
Serein.Proto.WebSocket/Handle/WebSocketMsgContext.cs
Normal file
115
Serein.Proto.WebSocket/Handle/WebSocketMsgContext.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Proto.WebSocket.Handle
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 消息处理上下文
|
||||
/// </summary>
|
||||
public class WebSocketMsgContext : IDisposable
|
||||
{
|
||||
public WebSocketMsgContext(Func<string, Task> sendAsync)
|
||||
{
|
||||
_sendAsync = sendAsync;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
MsgRequest = null;
|
||||
MsgTheme = null;
|
||||
MsgId = null;
|
||||
MsgData = null;
|
||||
MsgData = null;
|
||||
_sendAsync = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标记是否已经处理,如果是,则提前退出
|
||||
/// </summary>
|
||||
public bool Handle { get => _handle; set{
|
||||
if(value)
|
||||
{
|
||||
Dispose();
|
||||
_handle = value;
|
||||
}
|
||||
} }
|
||||
|
||||
public bool _handle = false;
|
||||
|
||||
/// <summary>
|
||||
/// 消息本体(IJsonToken)
|
||||
/// </summary>
|
||||
public IJsonToken MsgRequest { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息请求的主题
|
||||
/// </summary>
|
||||
public string MsgTheme { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息附带的ID
|
||||
/// </summary>
|
||||
public string MsgId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息的数据
|
||||
/// </summary>
|
||||
public IJsonToken MsgData { get; set; }
|
||||
|
||||
|
||||
private Func<string, Task> _sendAsync;
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendAsync(string msg)
|
||||
{
|
||||
await _sendAsync.Invoke(msg);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 返回消息
|
||||
/// </summary>
|
||||
/// <param name="moduleConfig"></param>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public async Task RepliedAsync(WebSocketHandleModuleConfig moduleConfig,
|
||||
WebSocketMsgContext context,
|
||||
object data)
|
||||
{
|
||||
if (moduleConfig.IsResponseUseReturn)
|
||||
{
|
||||
var responseContent = JsonHelper.Serialize(data);
|
||||
await SendAsync(responseContent);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
IJsonToken jsonData;
|
||||
|
||||
jsonData = JsonHelper.Object(obj =>
|
||||
{
|
||||
obj[moduleConfig.MsgIdJsonKey] = context.MsgId;
|
||||
obj[moduleConfig.ThemeJsonKey] = context.MsgTheme;
|
||||
obj[moduleConfig.DataJsonKey] = data is null ? null
|
||||
: JsonHelper.FromObject(data);
|
||||
});
|
||||
|
||||
var msg = jsonData.ToString();
|
||||
//Console.WriteLine($"[{msgId}] => {theme}");
|
||||
await SendAsync(msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
212
Serein.Proto.WebSocket/Handle/WebSocketMsgHandleHelper.cs
Normal file
212
Serein.Proto.WebSocket/Handle/WebSocketMsgHandleHelper.cs
Normal file
@@ -0,0 +1,212 @@
|
||||
using Serein.Library;
|
||||
using Serein.Proto.WebSocket.Attributes;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Serein.Proto.WebSocket.Handle
|
||||
{
|
||||
/// <summary>
|
||||
/// 适用于Json数据格式的WebSocket消息处理类
|
||||
/// </summary>
|
||||
public class WebSocketMsgHandleHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// (Theme Name ,Data Name) - HandleModule
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<(string, string), WebSocketHandleModule> MyHandleModuleDict
|
||||
= new ConcurrentDictionary<(string, string), WebSocketHandleModule>();
|
||||
|
||||
private Action<Exception, Action<object>> _onExceptionTracking;
|
||||
/// <summary>
|
||||
/// 异常跟踪
|
||||
/// </summary>
|
||||
public event Action<Exception, Action<object>> OnExceptionTracking;
|
||||
|
||||
/// <summary>
|
||||
/// 添加消息处理与异常处理
|
||||
/// </summary>
|
||||
/// <param name="moduleConfig">模块配置</param>
|
||||
/// <returns></returns>
|
||||
private WebSocketHandleModule AddMyHandleModule(WebSocketHandleModuleConfig moduleConfig)
|
||||
{
|
||||
var key = (moduleConfig.ThemeJsonKey, moduleConfig.DataJsonKey);
|
||||
if (!MyHandleModuleDict.TryGetValue(key, out var myHandleModule))
|
||||
{
|
||||
myHandleModule = new WebSocketHandleModule(moduleConfig);
|
||||
MyHandleModuleDict[key] = myHandleModule;
|
||||
}
|
||||
return myHandleModule;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除某个模块的WebSocket消息处理
|
||||
/// </summary>
|
||||
/// <param name="socketControlBase"></param>
|
||||
public void RemoveModule(ISocketHandleModule socketControlBase)
|
||||
{
|
||||
var type = socketControlBase.GetType();
|
||||
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
|
||||
if (moduleAttribute is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var themeKeyName = moduleAttribute.ThemeKey;
|
||||
var dataKeyName = moduleAttribute.DataKey;
|
||||
var key = (themeKeyName, dataKeyName);
|
||||
if (MyHandleModuleDict.TryGetValue(key, out var myHandleModules))
|
||||
{
|
||||
var isRemote = myHandleModules.RemoveConfig(socketControlBase);
|
||||
if (isRemote) MyHandleModuleDict.TryGetValue(key, out _);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加消息处理以及异常处理
|
||||
/// </summary>
|
||||
/// <param name="socketControlBase"></param>
|
||||
/// <param name="onExceptionTracking"></param>
|
||||
public void AddModule<T>(Func<ISocketHandleModule> instanceFactory, Action<Exception, Action<object>> onExceptionTracking)
|
||||
where T : ISocketHandleModule
|
||||
{
|
||||
var type = typeof(T);
|
||||
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
|
||||
if (moduleAttribute is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var themeKey = moduleAttribute.ThemeKey;
|
||||
var dataKey = moduleAttribute.DataKey;
|
||||
var msgIdKey = moduleAttribute.MsgIdKey;
|
||||
var isResponseUseReturn = moduleAttribute.IsResponseUseReturn;
|
||||
var moduleConfig = new WebSocketHandleModuleConfig()
|
||||
{
|
||||
ThemeJsonKey = themeKey,
|
||||
DataJsonKey = dataKey,
|
||||
MsgIdJsonKey = msgIdKey,
|
||||
IsResponseUseReturn = isResponseUseReturn,
|
||||
};
|
||||
|
||||
var handleModule = AddMyHandleModule(moduleConfig);
|
||||
var configs = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Select<MethodInfo, WebSocketHandleConfiguration>(methodInfo =>
|
||||
{
|
||||
var methodsAttribute = methodInfo.GetCustomAttribute<AutoSocketHandleAttribute>();
|
||||
if (methodsAttribute is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(methodsAttribute.ThemeValue))
|
||||
{
|
||||
methodsAttribute.ThemeValue = methodInfo.Name;
|
||||
}
|
||||
|
||||
#region 生成处理配置
|
||||
var config = new WebSocketHandleConfiguration();
|
||||
|
||||
config.ThemeValue = methodsAttribute.ThemeValue;
|
||||
config.ArgNotNull = methodsAttribute.ArgNotNull;
|
||||
config.IsReturnValue = methodsAttribute.IsReturnValue;
|
||||
//if (config.IsReturnValue)
|
||||
//{
|
||||
// // 重新检查是否能够返回
|
||||
// if (methodInfo.ReturnType == typeof(void))
|
||||
// {
|
||||
// config.IsReturnValue = false; // void 不返回
|
||||
// }
|
||||
// else if (methodInfo.ReturnType == typeof(Unit))
|
||||
// {
|
||||
// config.IsReturnValue = false; // Unit 不返回
|
||||
// }
|
||||
// else if (methodInfo.ReturnType == typeof(Task))
|
||||
// {
|
||||
// config.IsReturnValue = false; // void 不返回
|
||||
// }
|
||||
|
||||
//}
|
||||
var parameterInfos = methodInfo.GetParameters();
|
||||
|
||||
config.DelegateDetails = new DelegateDetails(methodInfo); // 对应theme的emit构造委托调用工具类
|
||||
config.InstanceFactory = instanceFactory; // 调用emit委托时的实例
|
||||
config.OnExceptionTracking = onExceptionTracking; // 异常追踪
|
||||
config.ParameterType = parameterInfos.Select(t => t.ParameterType).ToArray(); // 入参参数类型
|
||||
config.ParameterName = parameterInfos.Select(t => t.Name).ToArray(); // 入参参数名称
|
||||
config.UseRequest = parameterInfos.Select(p => p.GetCustomAttribute<UseRequestAttribute>() != null).ToArray(); // 是否使用整体data数据
|
||||
config.UseData = parameterInfos.Select(p => p.GetCustomAttribute<UseDataAttribute>() != null).ToArray(); // 是否使用整体data数据
|
||||
config.UseMsgId = parameterInfos.Select(p => p.GetCustomAttribute<UseMsgIdAttribute>() != null).ToArray(); // 是否使用消息ID
|
||||
#if NET5_0_OR_GREATER
|
||||
config.IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NotNullAttribute>() != null).ToArray(); // 是否检查null
|
||||
#endif
|
||||
|
||||
if (config.IsCheckArgNotNull is null)
|
||||
{
|
||||
config.IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NeedfulAttribute>() != null).ToArray(); // 是否检查null
|
||||
}
|
||||
else
|
||||
{
|
||||
// 兼容两种非空特性的写法
|
||||
var argNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NeedfulAttribute>() != null).ToArray(); // 是否检查null
|
||||
for (int i = 0; i < config.IsCheckArgNotNull.Length; i++)
|
||||
{
|
||||
if (!config.IsCheckArgNotNull[i] && argNotNull[i])
|
||||
{
|
||||
config.IsCheckArgNotNull[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
var value = methodsAttribute.ThemeValue;
|
||||
|
||||
return config;
|
||||
}
|
||||
})
|
||||
.Where(config => config != null).ToList();
|
||||
if (configs.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
SereinEnv.WriteLine(InfoType.INFO, $"add websocket handle model :");
|
||||
SereinEnv.WriteLine(InfoType.INFO, $"theme key, data key : {themeKey}, {dataKey}");
|
||||
foreach (var config in configs)
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.INFO, $"theme value : {config.ThemeValue} ");
|
||||
var result = handleModule.AddHandleConfigs(config);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步处理消息
|
||||
/// </summary>
|
||||
/// <param name="context">此次请求的上下文</param>
|
||||
/// <returns></returns>
|
||||
public void Handle(WebSocketMsgContext context)
|
||||
{
|
||||
foreach (var module in MyHandleModuleDict.Values)
|
||||
{
|
||||
if (context.Handle)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_ = module.HandleAsync(context);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
10
Serein.Proto.WebSocket/ISocketHandleModule.cs
Normal file
10
Serein.Proto.WebSocket/ISocketHandleModule.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace Serein.Proto.WebSocket
|
||||
{
|
||||
public interface ISocketHandleModule
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
14
Serein.Proto.WebSocket/Serein.Proto.WebSocket.csproj
Normal file
14
Serein.Proto.WebSocket/Serein.Proto.WebSocket.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;net462</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Library\Serein.Library.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
93
Serein.Proto.WebSocket/TestExtension.cs
Normal file
93
Serein.Proto.WebSocket/TestExtension.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Channels;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Proto.WebSocket
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息处理工具
|
||||
/// </summary>
|
||||
public class MsgHandleUtil
|
||||
{
|
||||
private readonly Channel<string> _msgChannel;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化优先容器
|
||||
/// </summary>
|
||||
/// <param name="capacity"></param>
|
||||
public MsgHandleUtil(int capacity = 100)
|
||||
{
|
||||
_msgChannel = Channel.CreateBounded<string>(new BoundedChannelOptions(capacity)
|
||||
{
|
||||
FullMode = BoundedChannelFullMode.Wait
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 等待消息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<string> WaitMsgAsync()
|
||||
{
|
||||
// 检查是否可以读取消息
|
||||
if (await _msgChannel.Reader.WaitToReadAsync())
|
||||
{
|
||||
return await _msgChannel.Reader.ReadAsync();
|
||||
}
|
||||
return null; // 若通道关闭,则返回null
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入消息
|
||||
/// </summary>
|
||||
/// <param name="msg">消息内容</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public async Task<bool> WriteMsgAsync(string msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _msgChannel.Writer.WriteAsync(msg);
|
||||
return true;
|
||||
}
|
||||
catch (ChannelClosedException)
|
||||
{
|
||||
// Channel 已关闭
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试关闭通道,停止写入消息
|
||||
/// </summary>
|
||||
public void CloseChannel()
|
||||
{
|
||||
_msgChannel.Writer.Complete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class SocketExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="webSocket"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task SendAsync(System.Net.WebSockets.WebSocket webSocket, string message)
|
||||
{
|
||||
var buffer = Encoding.UTF8.GetBytes(message);
|
||||
await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
214
Serein.Proto.WebSocket/WebSocketClient.cs
Normal file
214
Serein.Proto.WebSocket/WebSocketClient.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
using Serein.Library.Utils;
|
||||
using Serein.Proto.WebSocket.Handle;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Proto.WebSocket
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// WebSocket客户端
|
||||
/// </summary>
|
||||
public class WebSocketClient
|
||||
{
|
||||
/// <summary>
|
||||
/// WebSocket客户端
|
||||
/// </summary>
|
||||
public WebSocketClient()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 消息处理
|
||||
/// </summary>
|
||||
public WebSocketMsgHandleHelper MsgHandleHelper { get; } = new WebSocketMsgHandleHelper();
|
||||
|
||||
private ClientWebSocket _client = new ClientWebSocket();
|
||||
|
||||
/// <summary>
|
||||
/// 连接到指定WebSocket Server服务
|
||||
/// </summary>
|
||||
/// <param name="uri"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> ConnectAsync(string uri)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _client.ConnectAsync(new Uri(uri), CancellationToken.None);
|
||||
_ = ReceiveAsync();
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendAsync(string message)
|
||||
{
|
||||
await SocketExtension.SendAsync(_client, message); // 回复客户端
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始处理消息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
|
||||
var msgQueueUtil = new MsgHandleUtil();
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await HandleMsgAsync(_client, msgQueueUtil);
|
||||
});
|
||||
|
||||
|
||||
var receivedMessage = new StringBuilder(); // 用于拼接长消息
|
||||
|
||||
while (_client.State == WebSocketState.Open)
|
||||
{
|
||||
try
|
||||
{
|
||||
var buffer = new byte[1024];
|
||||
WebSocketReceiveResult result;
|
||||
|
||||
do
|
||||
{
|
||||
result = await _client.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
|
||||
// 根据接收到的字节数解码为部分字符串,并添加到 StringBuilder 中
|
||||
var partialMessage = Encoding.UTF8.GetString(buffer, 0, result.Count);
|
||||
receivedMessage.Append(partialMessage);
|
||||
|
||||
} while (!result.EndOfMessage); // 判断是否已经收到完整消息
|
||||
var message = receivedMessage.ToString();
|
||||
await msgQueueUtil.WriteMsgAsync(message);
|
||||
receivedMessage.Clear(); // 清空 StringBuilder 为下一条消息做准备
|
||||
// 处理收到的完整消息
|
||||
if (result.MessageType == WebSocketMessageType.Close)
|
||||
{
|
||||
await _client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
|
||||
}
|
||||
//else
|
||||
//{
|
||||
// var completeMessage = receivedMessage.ToString();
|
||||
// MsgHandleHelper.HandleMsg(SendAsync, completeMessage); // 处理消息,如果方法入参是需要发送消息委托时,将 SendAsync 作为委托参数提供
|
||||
// //Debug.WriteLine($"Received: {completeMessage}");
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"Received: {ex.ToString()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task HandleMsgAsync(System.Net.WebSockets.WebSocket webSocket, MsgHandleUtil msgQueueUtil)
|
||||
{
|
||||
async Task sendasync(string text)
|
||||
{
|
||||
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
var message = await msgQueueUtil.WaitMsgAsync(); // 有消息时通知
|
||||
var context = new WebSocketMsgContext(sendasync);
|
||||
context.MsgRequest = JsonHelper.Parse(message);
|
||||
MsgHandleHelper.Handle(context); // 处理消息
|
||||
|
||||
//using (var context = new WebSocketMsgContext(sendasync))
|
||||
//{
|
||||
// context.JsonObject = JObject.Parse(message);
|
||||
// await MsgHandleHelper.HandleAsync(context); // 处理消息
|
||||
//}
|
||||
|
||||
//_ = Task.Run(() => {
|
||||
// JObject json = JObject.Parse(message);
|
||||
// WebSocketMsgContext context = new WebSocketMsgContext(async (text) =>
|
||||
// {
|
||||
// await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
// });
|
||||
// context.JsonObject = json;
|
||||
// await MsgHandleHelper.HandleAsync(context); // 处理消息
|
||||
//});
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* #region 消息处理
|
||||
private readonly string ThemeField;
|
||||
private readonly ConcurrentDictionary<string, HandldConfig> ThemeConfigs = new ConcurrentDictionary<string, HandldConfig>();
|
||||
|
||||
public async Task HandleSocketMsg(string jsonStr)
|
||||
{
|
||||
JObject json;
|
||||
try
|
||||
{
|
||||
json = JObject.Parse(jsonStr);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await SendAsync(_client, ex.Message);
|
||||
return;
|
||||
}
|
||||
// 获取到消息
|
||||
string themeName = json[ThemeField]?.ToString();
|
||||
if (!ThemeConfigs.TryGetValue(themeName, out var handldConfig))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
object dataValue;
|
||||
if (string.IsNullOrEmpty(handldConfig.DataField))
|
||||
{
|
||||
dataValue = json.ToObject(handldConfig.DataType);
|
||||
}
|
||||
else
|
||||
{
|
||||
dataValue = json[handldConfig.DataField].ToObject(handldConfig.DataType);
|
||||
}
|
||||
await handldConfig.Invoke(dataValue, SendAsync);
|
||||
}
|
||||
|
||||
public void AddConfig(string themeName, Type dataType, MsgHandler msgHandler)
|
||||
{
|
||||
if (!ThemeConfigs.TryGetValue(themeName, out var handldConfig))
|
||||
{
|
||||
handldConfig = new HandldConfig
|
||||
{
|
||||
DataField = themeName,
|
||||
DataType = dataType
|
||||
};
|
||||
ThemeConfigs.TryAdd(themeName, handldConfig);
|
||||
}
|
||||
handldConfig.HandldAsync += msgHandler;
|
||||
}
|
||||
public void RemoteConfig(string themeName, MsgHandler msgHandler)
|
||||
{
|
||||
if (ThemeConfigs.TryGetValue(themeName, out var handldConfig))
|
||||
{
|
||||
handldConfig.HandldAsync -= msgHandler;
|
||||
if (!handldConfig.HasSubscribers)
|
||||
{
|
||||
ThemeConfigs.TryRemove(themeName, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion*/
|
||||
}
|
||||
}
|
||||
}
|
||||
282
Serein.Proto.WebSocket/WebSocketServer.cs
Normal file
282
Serein.Proto.WebSocket/WebSocketServer.cs
Normal file
@@ -0,0 +1,282 @@
|
||||
using Serein.Library;
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Utils;
|
||||
using Serein.Proto.WebSocket.Handle;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Proto.WebSocket
|
||||
{
|
||||
/// <summary>
|
||||
/// WebSocket JSON 消息授权管理
|
||||
/// </summary>
|
||||
public class WebSocketAuthorizedHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// WebSocket JSON 消息授权管理
|
||||
/// </summary>
|
||||
public WebSocketAuthorizedHelper(string addresPort,string token, Func<dynamic, Task<bool>> inspectionAuthorizedFunc)
|
||||
{
|
||||
AddresPort = addresPort;
|
||||
TokenKey = token;
|
||||
InspectionAuthorizedFunc = inspectionAuthorizedFunc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 客户端地址
|
||||
/// </summary>
|
||||
public string AddresPort { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 授权字段
|
||||
/// </summary>
|
||||
private readonly string TokenKey;
|
||||
|
||||
/// <summary>
|
||||
/// 处理消息授权事件
|
||||
/// </summary>
|
||||
private readonly Func<dynamic, Task<bool>> InspectionAuthorizedFunc;
|
||||
|
||||
private SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1);
|
||||
|
||||
/// <summary>
|
||||
/// 处理消息授权
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public async Task<bool> HandleAuthorized(string message)
|
||||
{
|
||||
await semaphoreSlim.WaitAsync(1);
|
||||
bool isAuthorized = false;
|
||||
IJsonToken json = JsonHelper.Parse(message);
|
||||
if(json.TryGetValue(TokenKey,out var token))
|
||||
{
|
||||
// 交给之前定义的授权方法进行判断
|
||||
isAuthorized = await InspectionAuthorizedFunc?.Invoke(token);
|
||||
}
|
||||
else
|
||||
{
|
||||
isAuthorized = false;
|
||||
}
|
||||
return isAuthorized;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// WebSocket服务类
|
||||
/// </summary>
|
||||
[AutoRegister]
|
||||
public class WebSocketServer
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息处理
|
||||
/// </summary>
|
||||
public WebSocketMsgHandleHelper MsgHandleHelper { get; } = new WebSocketMsgHandleHelper();
|
||||
|
||||
private HttpListener listener;
|
||||
|
||||
/// <summary>
|
||||
/// 创建无须授权验证的WebSocket服务端
|
||||
/// </summary>
|
||||
public WebSocketServer()
|
||||
{
|
||||
AuthorizedClients = new ConcurrentDictionary<string, WebSocketAuthorizedHelper>();
|
||||
InspectionAuthorizedFunc = (tokenObj) => Task.FromResult(true);
|
||||
IsCheckToken = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建需要授权验证的WebSocket服务端
|
||||
/// </summary>
|
||||
/// <param name="tokenKey">token 字段</param>
|
||||
/// <param name="inspectionAuthorizedFunc">验证token的方法</param>
|
||||
public WebSocketServer(string tokenKey, Func<dynamic, Task<bool>> inspectionAuthorizedFunc)
|
||||
{
|
||||
TokenKey = tokenKey;
|
||||
AuthorizedClients = new ConcurrentDictionary<string, WebSocketAuthorizedHelper>();
|
||||
InspectionAuthorizedFunc = inspectionAuthorizedFunc;
|
||||
IsCheckToken = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 授权
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<string, WebSocketAuthorizedHelper> AuthorizedClients;
|
||||
private readonly string TokenKey;
|
||||
private readonly Func<dynamic, Task<bool>> InspectionAuthorizedFunc;
|
||||
private bool IsCheckToken = false;
|
||||
/// <summary>
|
||||
/// 进行监听服务
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
public async Task StartAsync(string url)
|
||||
{
|
||||
listener = new HttpListener();
|
||||
listener.Prefixes.Add(url);
|
||||
await Console.Out.WriteLineAsync($"WebSocket消息处理已启动[{url}]");
|
||||
try
|
||||
{
|
||||
listener.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync(ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var context = await listener.GetContextAsync();
|
||||
string clientPoint = context.Request.RemoteEndPoint?.ToString();
|
||||
|
||||
await Console.Out.WriteLineAsync($"新的连接加入:{clientPoint}");
|
||||
|
||||
if (context.Request.IsWebSocketRequest)
|
||||
{
|
||||
WebSocketAuthorizedHelper authorizedHelper = null;
|
||||
if (IsCheckToken)
|
||||
{
|
||||
if (AuthorizedClients.TryAdd(clientPoint, new WebSocketAuthorizedHelper(clientPoint, TokenKey, InspectionAuthorizedFunc)))
|
||||
{
|
||||
AuthorizedClients.TryGetValue(clientPoint, out authorizedHelper);
|
||||
}
|
||||
}
|
||||
|
||||
var webSocketContext = await context.AcceptWebSocketAsync(null); //新连接
|
||||
_ = HandleWebSocketAsync(webSocketContext.WebSocket, authorizedHelper); // 处理消息
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync(ex.Message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止监听服务
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
listener?.Stop();
|
||||
}
|
||||
|
||||
private async Task HandleWebSocketAsync(System.Net.WebSockets.WebSocket webSocket, WebSocketAuthorizedHelper authorizedHelper)
|
||||
{
|
||||
// 需要授权,却没有成功创建授权类,关闭连接
|
||||
if (IsCheckToken && authorizedHelper is null)
|
||||
{
|
||||
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
|
||||
return;
|
||||
}
|
||||
|
||||
var msgQueueUtil = new MsgHandleUtil();
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await HandleMsgAsync(webSocket,msgQueueUtil, authorizedHelper);
|
||||
});
|
||||
|
||||
//Func<string, Task> SendAsync = async (text) =>
|
||||
//{
|
||||
// await WebSocketServer.SendAsync(webSocket, text);
|
||||
//};
|
||||
|
||||
var receivedMessage = new StringBuilder(); // 用于拼接长消息
|
||||
|
||||
while ( webSocket.State == WebSocketState.Open)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
WebSocketReceiveResult result;
|
||||
var buffer = new byte[1024];
|
||||
do
|
||||
{
|
||||
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
if (result.MessageType == WebSocketMessageType.Close)
|
||||
{
|
||||
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
|
||||
if (IsCheckToken)
|
||||
{
|
||||
AuthorizedClients.TryRemove(authorizedHelper.AddresPort, out var _);
|
||||
}
|
||||
}
|
||||
// 将接收到的部分消息解码并拼接
|
||||
var partialMessage = Encoding.UTF8.GetString(buffer, 0, result.Count);
|
||||
receivedMessage.Append(partialMessage);
|
||||
|
||||
} while (!result.EndOfMessage); // 循环直到接收到完整的消息
|
||||
// 完整消息已经接收到,准备处理
|
||||
var message = receivedMessage.ToString(); // 获取消息文本
|
||||
receivedMessage.Clear(); // 清空 StringBuilder 为下一条消息做准备
|
||||
await msgQueueUtil.WriteMsgAsync(message); // 处理消息
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 处理异常
|
||||
Debug.WriteLine($"Error: {ex.ToString()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task HandleMsgAsync(System.Net.WebSockets.WebSocket webSocket,
|
||||
MsgHandleUtil msgQueueUtil,
|
||||
WebSocketAuthorizedHelper authorizedHelper)
|
||||
{
|
||||
async Task sendasync(string text)
|
||||
{
|
||||
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
var message = await msgQueueUtil.WaitMsgAsync(); // 有消息时通知
|
||||
if (IsCheckToken)
|
||||
{
|
||||
var authorizedResult = await authorizedHelper.HandleAuthorized(message); // 尝试检测授权
|
||||
if (!authorizedResult) // 授权失败
|
||||
{
|
||||
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
|
||||
if (IsCheckToken)
|
||||
{
|
||||
AuthorizedClients.TryRemove(authorizedHelper.AddresPort, out var _);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
var context = new WebSocketMsgContext(sendasync);
|
||||
context.MsgRequest = JsonHelper.Parse(message);
|
||||
MsgHandleHelper.Handle(context); // 处理消息
|
||||
|
||||
//using (var context = new WebSocketMsgContext(sendasync))
|
||||
//{
|
||||
// context.JsonObject = JObject.Parse(message);
|
||||
// await MsgHandleHelper.Handle(context); // 处理消息
|
||||
//}
|
||||
//_ = Task.Run(() => {
|
||||
|
||||
|
||||
//});
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user