mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-07 02:00:47 +08:00
更改了远程环境下websocket会来回发送重复消息的问题
This commit is contained in:
@@ -25,8 +25,17 @@ namespace Serein.Library.Network.WebSocketCommunication
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public sealed class AutoSocketModuleAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 业务标识
|
||||
/// </summary>
|
||||
public string ThemeKey;
|
||||
/// <summary>
|
||||
/// 数据标识
|
||||
/// </summary>
|
||||
public string DataKey;
|
||||
/// <summary>
|
||||
/// ID标识
|
||||
/// </summary>
|
||||
public string MsgIdKey;
|
||||
}
|
||||
|
||||
@@ -55,10 +64,10 @@ namespace Serein.Library.Network.WebSocketCommunication
|
||||
public string ThemeValue = string.Empty;
|
||||
/// <summary>
|
||||
/// <para>标记方法执行完成后是否需要将结果发送。</para>
|
||||
/// <para>但以下情况将不会发送:</para>
|
||||
/// <para>1.返回类型为void</para>
|
||||
/// <para>2.返回类型为Task</para>
|
||||
/// <para>3.返回了null</para>
|
||||
/// <para>注意以下返回值,返回的 json 中将不会新建 DataKey 字段:</para>
|
||||
/// <para>1.返回类型为 void </para>
|
||||
/// <para>2.返回类型为 Task </para>
|
||||
/// <para>2.返回类型为 Unit </para>
|
||||
/// <para>补充:如果返回类型是Task<TResult></para>
|
||||
/// <para>会进行异步等待,当Task结束后,自动获取TResult进行发送(请避免Task<Task<TResult>>诸如此类的Task泛型嵌套)</para>
|
||||
/// </summary>
|
||||
|
||||
@@ -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; ; // 表示是否可以调用方法
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -129,21 +129,28 @@ namespace Serein.Library.Network.WebSocketCommunication
|
||||
public async Task HandleMsgAsync(WebSocket webSocket,
|
||||
MsgQueueUtil msgQueueUtil)
|
||||
{
|
||||
|
||||
async Task sendasync(string text)
|
||||
{
|
||||
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
var message = await msgQueueUtil.WaitMsgAsync(); // 有消息时通知
|
||||
using (var context = new WebSocketMsgContext(sendasync))
|
||||
{
|
||||
context.JsonObject = JObject.Parse(message);
|
||||
await MsgHandleHelper.HandleAsync(context); // 处理消息
|
||||
}
|
||||
|
||||
|
||||
_ = Task.Run(() => {
|
||||
JObject json = JObject.Parse(message);
|
||||
WebSocketMsgContext context = new WebSocketMsgContext(async (text) =>
|
||||
{
|
||||
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
});
|
||||
context.JsonObject = json;
|
||||
MsgHandleHelper.HandleMsg(context); // 处理消息
|
||||
});
|
||||
//_ = Task.Run(() => {
|
||||
// JObject json = JObject.Parse(message);
|
||||
// WebSocketMsgContext context = new WebSocketMsgContext(async (text) =>
|
||||
// {
|
||||
// await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
// });
|
||||
// context.JsonObject = json;
|
||||
// await MsgHandleHelper.HandleAsync(context); // 处理消息
|
||||
//});
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -234,7 +234,10 @@ namespace Serein.Library.Network.WebSocketCommunication
|
||||
MsgQueueUtil msgQueueUtil,
|
||||
WebSocketAuthorizedHelper authorizedHelper)
|
||||
{
|
||||
|
||||
async Task sendasync(string text)
|
||||
{
|
||||
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
var message = await msgQueueUtil.WaitMsgAsync(); // 有消息时通知
|
||||
@@ -251,16 +254,16 @@ namespace Serein.Library.Network.WebSocketCommunication
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_ = Task.Run(() => {
|
||||
JObject json = JObject.Parse(message);
|
||||
WebSocketMsgContext context = new WebSocketMsgContext(async (text) =>
|
||||
{
|
||||
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
|
||||
});
|
||||
context.JsonObject = json;
|
||||
MsgHandleHelper.HandleMsg(context); // 处理消息
|
||||
});
|
||||
|
||||
using (var context = new WebSocketMsgContext(sendasync))
|
||||
{
|
||||
context.JsonObject = JObject.Parse(message);
|
||||
await MsgHandleHelper.HandleAsync(context); // 处理消息
|
||||
}
|
||||
//_ = Task.Run(() => {
|
||||
|
||||
|
||||
//});
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user