using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WebSocket4Net; namespace CoreAgent.ProtocolClient.Managers.WebSocketMgr { public partial class WebSocketMessageManager { #region IDisposable实现 /// /// 释放资源 /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// 释放资源的受保护方法 /// protected virtual void Dispose(bool disposing) { if (_disposed) return; if (disposing) { try { _logger.LogInformation($"[{_clientName}] 释放WebSocket消息管理器资源"); // 停止消息发送定时器 StopMessageDeferTimer(); // 清空消息队列 ClearMessageQueue(); // 释放BlockingCollection资源 _messageFifo?.Dispose(); // 关闭WebSocket连接 if (_webSocket != null) { _webSocket.Close(); _webSocket = null; } // 释放MessageIdManager _messageIdManager?.Dispose(); } catch (Exception ex) { _logger.LogError(ex, $"[{_clientName}] 释放资源异常: {ex.Message}"); } } _disposed = true; } #endregion } }