2024-10-10 10:45:53 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
|
|
|
|
|
{
|
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-10 20:52:19 +08:00
|
|
|
|
public WebSocketHandleModule(string ThemeJsonKey, string DataJsonKey)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
this.ThemeJsonKey = ThemeJsonKey;
|
|
|
|
|
|
this.DataJsonKey = DataJsonKey;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 指示处理模块该使用json中的哪个key作为业务区别字段
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string ThemeJsonKey { get; }
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 指示处理模块该使用json中的哪个key作为业务数据字段
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string DataJsonKey { get; }
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存储处理数据的配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ConcurrentDictionary<string, JsonMsgHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, JsonMsgHandleConfig>();
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
2024-10-10 20:52:19 +08:00
|
|
|
|
internal void AddHandleConfigs(SocketHandleModel model, ISocketHandleModule instance, MethodInfo methodInfo
|
2024-10-10 16:49:37 +08:00
|
|
|
|
, Action<Exception, Action<object>> onExceptionTracking)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-10 16:49:37 +08:00
|
|
|
|
if (!MyHandleConfigs.ContainsKey(model.ThemeValue))
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-15 10:55:41 +08:00
|
|
|
|
var jsonMsgHandleConfig = new JsonMsgHandleConfig(model,instance, methodInfo, onExceptionTracking);
|
|
|
|
|
|
MyHandleConfigs[model.ThemeValue] = jsonMsgHandleConfig;
|
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;
|
2024-10-10 16:49:37 +08:00
|
|
|
|
if (config.HandleGuid.Equals(socketControlBase.HandleGuid))
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
MyHandleConfigs.TryRemove(kv.Key, out _);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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();
|
|
|
|
|
|
foreach (var config in temp)
|
|
|
|
|
|
{
|
|
|
|
|
|
config.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理JSON数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="RecoverAsync"></param>
|
|
|
|
|
|
/// <param name="jsonObject"></param>
|
2024-10-10 10:45:53 +08:00
|
|
|
|
public void HandleSocketMsg(Func<string, Task> RecoverAsync, JObject jsonObject)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取到消息
|
|
|
|
|
|
string themeKeyName = jsonObject.GetValue(ThemeJsonKey)?.ToString();
|
|
|
|
|
|
if (!MyHandleConfigs.TryGetValue(themeKeyName, out var handldConfig))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 没有主题
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (jsonObject[DataJsonKey] is JObject dataJsonObject)
|
|
|
|
|
|
{
|
|
|
|
|
|
handldConfig.Handle(RecoverAsync, dataJsonObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|