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-10 20:52:19 +08:00
|
|
|
|
public class WebSocketHandleModule
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
public string ThemeJsonKey { get; }
|
|
|
|
|
|
public string DataJsonKey { get; }
|
|
|
|
|
|
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-10 20:52:19 +08:00
|
|
|
|
public ConcurrentDictionary<string, WebSocketHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, WebSocketHandleConfig>();
|
|
|
|
|
|
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-10 20:52:19 +08:00
|
|
|
|
var myHandleConfig = new WebSocketHandleConfig(model,instance, methodInfo, onExceptionTracking);
|
2024-10-10 16:49:37 +08:00
|
|
|
|
MyHandleConfigs[model.ThemeValue] = myHandleConfig;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-10 20:52:19 +08:00
|
|
|
|
public bool ResetConfig(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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ResetConfig()
|
|
|
|
|
|
{
|
|
|
|
|
|
var temp = MyHandleConfigs.Values;
|
|
|
|
|
|
MyHandleConfigs.Clear();
|
|
|
|
|
|
foreach (var config in temp)
|
|
|
|
|
|
{
|
|
|
|
|
|
config.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|