2024-10-20 12:10:57 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Reflection;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
using System.Text;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using System.Threading;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
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-20 12:10:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加处理配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="module">处理模块</param>
|
|
|
|
|
|
/// <param name="jsonMsgHandleConfig">处理配置</param>
|
|
|
|
|
|
internal bool AddHandleConfigs(SocketHandleModule module,JsonMsgHandleConfig jsonMsgHandleConfig)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-20 12:10:57 +08:00
|
|
|
|
if (!MyHandleConfigs.ContainsKey(module.ThemeValue))
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-20 12:10:57 +08:00
|
|
|
|
MyHandleConfigs[module.ThemeValue] = jsonMsgHandleConfig;
|
|
|
|
|
|
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;
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理JSON数据
|
|
|
|
|
|
/// </summary>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <param name="tSendAsync"></param>
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <param name="jsonObject"></param>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
public void HandleSocketMsg(Func<string, Task> tSendAsync, JObject jsonObject)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 获取到消息
|
|
|
|
|
|
string themeKeyName = jsonObject.GetValue(ThemeJsonKey)?.ToString();
|
|
|
|
|
|
if (!MyHandleConfigs.TryGetValue(themeKeyName, out var handldConfig))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 没有主题
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
Func<object, Task> SendAsync = async (data) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var sendMsg = new
|
|
|
|
|
|
{
|
|
|
|
|
|
theme = themeKeyName,
|
|
|
|
|
|
token = "",
|
|
|
|
|
|
data = data,
|
|
|
|
|
|
};
|
|
|
|
|
|
var msg = JsonConvert.SerializeObject(sendMsg);
|
|
|
|
|
|
await tSendAsync(msg);
|
|
|
|
|
|
};
|
|
|
|
|
|
try
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
JObject dataObj = jsonObject.GetValue(DataJsonKey).ToObject<JObject>();
|
|
|
|
|
|
handldConfig.Handle(SendAsync, dataObj);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"error in ws : {ex.Message}{Environment.NewLine}json value:{jsonObject}");
|
|
|
|
|
|
return;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|