From 856f8c245177f538b851371adb731fc324cf4e81 Mon Sep 17 00:00:00 2001 From: fengjiayi <12821976+ning_xi@user.noreply.gitee.com> Date: Mon, 25 Aug 2025 10:53:22 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=B8=BA=20WebSocket=20=E4=B8=AD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0Tag=E5=B1=9E=E6=80=A7,=E7=94=A8=E4=BA=8E=E5=86=8D?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E4=B8=AAws=E8=BF=9E=E6=8E=A5=E4=B8=AD?= =?UTF-8?q?=E5=85=B1=E7=94=A8=E6=9F=90=E4=BA=9B=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SereinWebSocketService.cs | 5 +- .../WebSocketHandleContext.cs | 89 +++++++++++++------ 2 files changed, 64 insertions(+), 30 deletions(-) diff --git a/Serein.Proto.WebSocket/SereinWebSocketService.cs b/Serein.Proto.WebSocket/SereinWebSocketService.cs index 277ebaa..4999ed0 100644 --- a/Serein.Proto.WebSocket/SereinWebSocketService.cs +++ b/Serein.Proto.WebSocket/SereinWebSocketService.cs @@ -335,10 +335,7 @@ namespace Serein.Proto.WebSocket return context; }, context => { - context.MsgRequest = null; - context.MsgData = null; - context.ErrorMessage = null; - context.Model = null; + context.Reset(); }); while (webSocket.State == WebSocketState.Open) diff --git a/Serein.Proto.WebSocket/WebSocketHandleContext.cs b/Serein.Proto.WebSocket/WebSocketHandleContext.cs index 2c0a4b9..ab7477f 100644 --- a/Serein.Proto.WebSocket/WebSocketHandleContext.cs +++ b/Serein.Proto.WebSocket/WebSocketHandleContext.cs @@ -2,6 +2,7 @@ using Serein.Library.Utils; using Serein.Proto.WebSocket.Handle; using System; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; @@ -11,7 +12,7 @@ namespace Serein.Proto.WebSocket /// /// 消息处理上下文 /// - public class WebSocketHandleContext : IDisposable + public class WebSocketHandleContext { /// /// 构造函数,传入发送消息的异步方法 @@ -23,34 +24,26 @@ namespace Serein.Proto.WebSocket } /// - /// 释放资源,清理消息上下文 + /// 重置上下文 /// - public void Dispose() + public void Reset() { MsgRequest = null; - /*MsgTheme = string.Empty; - MsgId = string.Empty; */ - MsgData = null; MsgData = null; + ErrorMessage = null; + Model = null; + IsError = false; } /// /// 标记是否已经处理,如果是,则提前退出 /// - public bool Handle { get => _handle; set{ - if(value) - { - Dispose(); - _handle = value; - } - } } - - private bool _handle = false; + public bool Handle { get; set; } /// /// WebSocket 模块配置 /// - public ModuleConfig Model { get; set; } + public ModuleConfig Model { get; set; } /// /// 消息本体(IJsonToken) @@ -62,16 +55,6 @@ namespace Serein.Proto.WebSocket /// public IJsonToken? MsgData { get; set; } - /// - /// 异常外部感知使能 - /// - public Action? OnExceptionTracking { get; set; } - - /// - /// 处理回复消息的函数 - /// - public Func OnReplyMakeData { get; set; } - public Action? OnReply { get; set; } /// /// 是否发生错误 @@ -83,12 +66,66 @@ namespace Serein.Proto.WebSocket /// public string ErrorMessage { get; set; } + + /// + /// 用于在同一个 Web Socket 中调用上下文中, 共享某个对象 + /// + private object? _wsTag; + private object _wsTagLockObj = new object(); + + /// + /// 设置共享对象(将在同一个 Web Socket 调起的上下文中保持一致) + /// + /// + /// + private void SetTag(T tag) + { + lock (_wsTagLockObj) + { + _wsTag = tag; + } + } + + /// + /// 获取共享对象(将在同一个 Web Socket 调起的上下文中保持一致) + /// + /// + /// + private bool TryGetTag([NotNullWhen(true)] out T? tag) + { + lock (_wsTagLockObj) + { + if (_wsTag is T t) + { + tag = t; + return true; + } + tag = default; + return false; + } + } + + /// + /// 异常外部感知使能 + /// + public Action? OnExceptionTracking { get; set; } + + /// + /// 处理回复消息的函数 + /// + public Func OnReplyMakeData { get; set; } + + /// + /// 获取回复内容的回调 + /// + public Action? OnReply { get; set; } /// /// 发送消息 /// private Func _sendAsync; + /// /// 发送消息 ///