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-22 00:13:13 +08:00
|
|
|
|
public WebSocketHandleModule(string themeJsonKey, string dataJsonKey, string msgIdJsonKey)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
this.ThemeJsonKey = themeJsonKey;
|
|
|
|
|
|
this.DataJsonKey = dataJsonKey;
|
|
|
|
|
|
this.MsgIdJsonKey = msgIdJsonKey;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// 指示处理模块该使用 Json 中的哪个 Key 作为业务区别字段
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string ThemeJsonKey { get; }
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// 指示处理模块该使用 Json 中的哪个 Key 作为业务数据字段
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string DataJsonKey { get; }
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 指示处理模块该使用 Json 中的哪个 Key 作为业务消息ID字段
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string MsgIdJsonKey { 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-22 00:13:13 +08:00
|
|
|
|
/// <param name="sendAsync"></param>
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <param name="jsonObject"></param>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void HandleSocketMsg(Func<string, Task> sendAsync, JObject jsonObject)
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 获取到消息
|
2024-10-22 00:13:13 +08:00
|
|
|
|
string theme = jsonObject.GetValue(ThemeJsonKey)?.ToString();
|
|
|
|
|
|
if (!MyHandleConfigs.TryGetValue(theme, out var handldConfig))
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 没有主题
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-10-22 00:13:13 +08:00
|
|
|
|
string msgId = jsonObject.GetValue(MsgIdJsonKey)?.ToString();
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
try
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
JObject dataObj = jsonObject.GetValue(DataJsonKey)?.ToObject<JObject>();
|
|
|
|
|
|
handldConfig.Handle(async (data) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await this.SendAsync(sendAsync, msgId, theme, data);
|
|
|
|
|
|
}, msgId, dataObj);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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-22 00:13:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sendAsync"></param>
|
|
|
|
|
|
/// <param name="msgId"></param>
|
|
|
|
|
|
/// <param name="theme"></param>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task SendAsync(Func<string, Task> sendAsync,string msgId, string theme, object data)
|
|
|
|
|
|
{
|
|
|
|
|
|
JObject jsonData;
|
|
|
|
|
|
|
|
|
|
|
|
if (data is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
jsonData = new JObject()
|
|
|
|
|
|
{
|
|
|
|
|
|
[MsgIdJsonKey] = msgId,
|
|
|
|
|
|
[ThemeJsonKey] = theme,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
JToken dataToken;
|
|
|
|
|
|
if ((data is System.Collections.IEnumerable || data is Array))
|
|
|
|
|
|
{
|
|
|
|
|
|
dataToken = JArray.FromObject(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dataToken = JObject.FromObject(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonData = new JObject()
|
|
|
|
|
|
{
|
|
|
|
|
|
[MsgIdJsonKey] = msgId,
|
|
|
|
|
|
[ThemeJsonKey] = theme,
|
|
|
|
|
|
[DataJsonKey] = dataToken
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
var msg = jsonData.ToString();
|
|
|
|
|
|
//Console.WriteLine(msg);
|
|
|
|
|
|
//Console.WriteLine();
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
await sendAsync.Invoke(msg);
|
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
|
|
|
|
}
|