更改了远程环境下websocket会来回发送重复消息的问题

This commit is contained in:
fengjiayi
2024-10-28 10:25:57 +08:00
parent e2f1ec5810
commit f20cfb755c
20 changed files with 297 additions and 167 deletions

View File

@@ -89,9 +89,8 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// <summary>
/// 处理JSON数据
/// </summary>
public async void HandleSocketMsg(WebSocketMsgContext context) // Func<string, Task> sendAsync, JObject jsonObject
public async Task HandleAsync(WebSocketMsgContext context)
{
var jsonObject = context.JsonObject; // 获取到消息
string theme = jsonObject.GetValue(moduleConfig.ThemeJsonKey)?.ToString();
if (!MyHandleConfigs.TryGetValue(theme, out var handldConfig))
@@ -112,7 +111,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
{
var dataObj = jsonObject.GetValue(moduleConfig.DataJsonKey)?.ToObject<JObject>();
context.MsgData = dataObj; // 添加消息
if (TryGetParameters(handldConfig, context, out var args))
if (WebSocketHandleModule.TryGetParameters(handldConfig, context, out var args))
{
var result = await WebSocketHandleModule.HandleAsync(handldConfig, args);
if (handldConfig.IsReturnValue)
@@ -149,7 +148,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// <param name="context">处理上下文</param>
/// <param name="args">返回的入参参数</param>
/// <returns></returns>
internal static bool TryGetParameters(HandleConfiguration config,WebSocketMsgContext context, out object[] args)
internal static bool TryGetParameters(HandleConfiguration config, WebSocketMsgContext context, out object[] args)
{
args = new object[config.ParameterType.Length];
bool isCanInvoke = true; ; // 表示是否可以调用方法

View File

@@ -11,23 +11,28 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// <summary>
/// 消息处理上下文
/// </summary>
public class WebSocketMsgContext
public class WebSocketMsgContext : IDisposable
{
public WebSocketMsgContext(Func<string, Task> sendAsync)
{
this._sendAsync = sendAsync;
}
public void Dispose()
{
JsonObject = null;
MsgTheme = null;
MsgId = null;
MsgData = null;
MsgData = null;
_sendAsync = null;
}
/// <summary>
/// 标记是否已经处理,如果是,则提前退出
/// </summary>
public bool Handle { get; set; }
/// <summary>
/// 消息本体(文本)
/// </summary>
public string Msg { get; set; }
/// <summary>
/// 消息本体JObject
/// </summary>
@@ -107,6 +112,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
await SendAsync(msg);
}
}
}

View File

@@ -17,6 +17,7 @@ using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
namespace Serein.Library.Network.WebSocketCommunication.Handle
{
@@ -118,14 +119,30 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
}
#region
var config = new WebSocketHandleConfiguration
{
IsReturnValue = methodsAttribute.IsReturnValue,
ThemeValue = methodsAttribute.ThemeValue,
ArgNotNull = methodsAttribute.ArgNotNull,
};
var parameterInfos = methodInfo.GetParameters();
var config = new WebSocketHandleConfiguration();
config.ThemeValue = methodsAttribute.ThemeValue;
config.ArgNotNull = methodsAttribute.ArgNotNull;
config.IsReturnValue = methodsAttribute.IsReturnValue;
//if (config.IsReturnValue)
//{
// // 重新检查是否能够返回
// if (methodInfo.ReturnType == typeof(void))
// {
// config.IsReturnValue = false; // void 不返回
// }
// else if (methodInfo.ReturnType == typeof(Unit))
// {
// config.IsReturnValue = false; // Unit 不返回
// }
// else if (methodInfo.ReturnType == typeof(Task))
// {
// config.IsReturnValue = false; // void 不返回
// }
//}
var parameterInfos = methodInfo.GetParameters();
config.DelegateDetails = new DelegateDetails(methodInfo); // 对应theme的emit构造委托调用工具类
config.Instance = socketControlBase; // 调用emit委托时的实例
config.OnExceptionTracking = onExceptionTracking; // 异常追踪
@@ -184,12 +201,11 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// </summary>
/// <param name="context">此次请求的上下文</param>
/// <returns></returns>
public void HandleMsg(WebSocketMsgContext context)
public async Task HandleAsync(WebSocketMsgContext context)
{
// OnExceptionTracking
foreach (var module in MyHandleModuleDict.Values)
{
module.HandleSocketMsg(context);
await module.HandleAsync(context);
}
}