2025-07-27 23:34:01 +08:00
|
|
|
|
using Serein.Library;
|
2024-11-08 17:30:51 +08:00
|
|
|
|
using Serein.Library.Utils;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2025-07-27 23:34:01 +08:00
|
|
|
|
namespace Serein.Proto.WebSocket.Handle
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Json消息处理模块
|
|
|
|
|
|
/// </summary>
|
2024-10-10 20:52:19 +08:00
|
|
|
|
public class WebSocketHandleModule
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Json消息处理模块
|
|
|
|
|
|
/// </summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
public WebSocketHandleModule(WebSocketHandleModuleConfig config)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
moduleConfig = config;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
/// 模块的处理配置
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// </summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
private readonly WebSocketHandleModuleConfig moduleConfig;
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
/// 用来判断消息是否重复
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// </summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
private HashSet<string> _myMsgIdHash = new HashSet<string>();
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存储处理数据的配置
|
|
|
|
|
|
/// </summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
public ConcurrentDictionary<string, HandleConfiguration> MyHandleConfigs = new ConcurrentDictionary<string, HandleConfiguration>();
|
|
|
|
|
|
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加处理配置
|
|
|
|
|
|
/// </summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
/// <param name="config">处理模块</param>
|
|
|
|
|
|
internal bool AddHandleConfigs(WebSocketHandleConfiguration config)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
if (!MyHandleConfigs.ContainsKey(config.ThemeValue))
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
MyHandleConfigs[config.ThemeValue] = config;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-15 10:55:41 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除某个处理模块
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="socketControlBase"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool RemoveConfig(ISocketHandleModule socketControlBase)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var kv in MyHandleConfigs.ToArray())
|
|
|
|
|
|
{
|
|
|
|
|
|
var config = kv.Value;
|
2025-07-27 23:34:01 +08:00
|
|
|
|
MyHandleConfigs.TryRemove(kv.Key, out _);
|
|
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
2024-10-10 16:49:37 +08:00
|
|
|
|
return MyHandleConfigs.Count == 0;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 卸载当前模块的所有配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void UnloadConfig()
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
var temp = MyHandleConfigs.Values;
|
|
|
|
|
|
MyHandleConfigs.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-28 00:31:41 +08:00
|
|
|
|
|
2024-10-27 00:54:10 +08:00
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理JSON数据
|
|
|
|
|
|
/// </summary>
|
2024-10-28 10:25:57 +08:00
|
|
|
|
public async Task HandleAsync(WebSocketMsgContext context)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
var jsonObject = context.MsgRequest; // 获取到消息
|
2024-10-28 00:31:41 +08:00
|
|
|
|
string theme = jsonObject.GetValue(moduleConfig.ThemeJsonKey)?.ToString();
|
2024-10-22 00:13:13 +08:00
|
|
|
|
if (!MyHandleConfigs.TryGetValue(theme, out var handldConfig))
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
return; // 没有主题
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
2024-10-28 00:31:41 +08:00
|
|
|
|
context.MsgTheme = theme; // 添加主题
|
|
|
|
|
|
string msgId = jsonObject.GetValue(moduleConfig.MsgIdJsonKey)?.ToString();
|
2024-10-27 00:54:10 +08:00
|
|
|
|
if (_myMsgIdHash.Contains(msgId))
|
|
|
|
|
|
{
|
2024-11-08 17:30:51 +08:00
|
|
|
|
SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 消息重复");
|
2024-10-27 00:54:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-10-28 00:31:41 +08:00
|
|
|
|
context.MsgId = msgId; // 添加 ID
|
2024-10-27 00:54:10 +08:00
|
|
|
|
_myMsgIdHash.Add(msgId);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
try
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
var dataObj = jsonObject.GetValue(moduleConfig.DataJsonKey);
|
2024-10-28 00:31:41 +08:00
|
|
|
|
context.MsgData = dataObj; // 添加消息
|
2025-07-27 23:34:01 +08:00
|
|
|
|
if (TryGetParameters(handldConfig, context, out var args))
|
2024-10-22 00:13:13 +08:00
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
var result = await HandleAsync(handldConfig, args);
|
2024-10-28 00:31:41 +08:00
|
|
|
|
if (handldConfig.IsReturnValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.RepliedAsync(moduleConfig, context, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-11-08 17:30:51 +08:00
|
|
|
|
SereinEnv.WriteLine(InfoType.ERROR, $"error in ws : {ex.Message}{Environment.NewLine}json value:{jsonObject}");
|
2024-10-28 15:21:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Handle = true;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
2024-10-22 00:13:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
/// 调用
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="config"></param>
|
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static async Task<object> HandleAsync(HandleConfiguration config, object[] args)
|
|
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
var instance = config.InstanceFactory.Invoke();
|
|
|
|
|
|
var result = await config.DelegateDetails.InvokeAsync(instance, args);
|
2024-10-28 00:31:41 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取入参参数
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// </summary>
|
2024-10-28 00:31:41 +08:00
|
|
|
|
/// <param name="config">处理配置</param>
|
|
|
|
|
|
/// <param name="context">处理上下文</param>
|
|
|
|
|
|
/// <param name="args">返回的入参参数</param>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <returns></returns>
|
2024-10-28 10:25:57 +08:00
|
|
|
|
internal static bool TryGetParameters(HandleConfiguration config, WebSocketMsgContext context, out object[] args)
|
2024-10-22 00:13:13 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
args = new object[config.ParameterType.Length];
|
|
|
|
|
|
bool isCanInvoke = true; ; // 表示是否可以调用方法
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
2024-10-28 00:31:41 +08:00
|
|
|
|
for (int i = 0; i < config.ParameterType.Length; i++)
|
2024-10-22 00:13:13 +08:00
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
|
2024-10-28 00:31:41 +08:00
|
|
|
|
var type = config.ParameterType[i]; // 入参变量类型
|
|
|
|
|
|
var argName = config.ParameterName[i]; // 入参参数名称
|
|
|
|
|
|
#region 传递消息ID
|
|
|
|
|
|
if (config.UseMsgId[i])
|
2024-10-22 00:13:13 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
args[i] = context.MsgId;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region DATA JSON数据
|
2025-07-27 23:34:01 +08:00
|
|
|
|
else if (config.UseRequest[i])
|
|
|
|
|
|
{
|
|
|
|
|
|
args[i] = context.MsgRequest.ToObject(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region DATA JSON数据
|
2024-10-28 00:31:41 +08:00
|
|
|
|
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))
|
2024-10-22 00:13:13 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
args[i] = jsonValue.ToObject(type);
|
2024-10-22 00:13:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
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);
|
2024-10-22 00:13:13 +08:00
|
|
|
|
}
|
2024-10-28 00:31:41 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (config.ArgNotNull && !config.IsCheckArgNotNull[i])
|
|
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
2024-10-28 00:31:41 +08:00
|
|
|
|
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 =>
|
|
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
var jsonText = JsonHelper.Serialize(data);
|
2024-10-28 00:31:41 +08:00
|
|
|
|
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 =>
|
|
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
var jsonText = JsonHelper.Serialize(data);
|
2024-10-28 00:31:41 +08:00
|
|
|
|
await context.SendAsync(jsonText);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type.IsAssignableFrom(typeof(Action<string>)))
|
2024-10-22 00:13:13 +08:00
|
|
|
|
{
|
2024-10-28 00:31:41 +08:00
|
|
|
|
args[i] = new Action<string>(async data =>
|
|
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
var jsonText = JsonHelper.Serialize(data);
|
2024-10-28 00:31:41 +08:00
|
|
|
|
await context.SendAsync(jsonText);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
2024-10-22 00:13:13 +08:00
|
|
|
|
}
|
2024-10-28 00:31:41 +08:00
|
|
|
|
return isCanInvoke;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|