You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

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实现
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 释放资源的受保护方法
/// </summary>
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
}
}