|
|
@ -20,7 +20,6 @@ namespace LTEMvcApp.Services |
|
|
|
#region 私有字段
|
|
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, LTEClientWebSocket> _clients; |
|
|
|
private readonly ConcurrentDictionary<string, ClientConfig> _configs; |
|
|
|
private readonly ILogger<WebSocketManagerService> _logger; |
|
|
|
private readonly IServiceProvider _serviceProvider; |
|
|
|
private List<ClientConfig> _testClientConfigs; // 只保留多个测试配置
|
|
|
@ -70,7 +69,6 @@ namespace LTEMvcApp.Services |
|
|
|
public WebSocketManagerService(ILogger<WebSocketManagerService> logger, IServiceProvider serviceProvider) |
|
|
|
{ |
|
|
|
_clients = new ConcurrentDictionary<string, LTEClientWebSocket>(); |
|
|
|
_configs = new ConcurrentDictionary<string, ClientConfig>(); |
|
|
|
_logger = logger; |
|
|
|
_serviceProvider = serviceProvider; |
|
|
|
_testClientConfigs = new List<ClientConfig>(); // 初始化测试配置列表
|
|
|
@ -180,44 +178,6 @@ namespace LTEMvcApp.Services |
|
|
|
|
|
|
|
#region 公共方法
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 添加客户端配置
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">客户端配置</param>
|
|
|
|
/// <returns>是否成功添加</returns>
|
|
|
|
public bool AddClientConfig(ClientConfig config) |
|
|
|
{ |
|
|
|
if (string.IsNullOrEmpty(config.Name)) |
|
|
|
{ |
|
|
|
_logger.LogWarning("尝试添加空名称客户端配置"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
_logger.LogInformation($"添加客户端配置: {config.Name}"); |
|
|
|
_configs[config.Name] = config; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 移除客户端配置
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="clientName">客户端名称</param>
|
|
|
|
/// <returns>是否成功移除</returns>
|
|
|
|
public bool RemoveClientConfig(string clientName) |
|
|
|
{ |
|
|
|
_logger.LogInformation($"移除客户端配置: {clientName}"); |
|
|
|
if (_configs.TryRemove(clientName, out _)) |
|
|
|
{ |
|
|
|
// 如果客户端正在运行,停止它
|
|
|
|
if (_clients.TryGetValue(clientName, out var client)) |
|
|
|
{ |
|
|
|
client.Stop(); |
|
|
|
_clients.TryRemove(clientName, out _); |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 启动客户端
|
|
|
|
/// </summary>
|
|
|
@ -234,7 +194,7 @@ namespace LTEMvcApp.Services |
|
|
|
config = testConfig; |
|
|
|
_logger.LogInformation($"使用测试客户端配置: {config.Address}"); |
|
|
|
} |
|
|
|
else if (!_configs.TryGetValue(address, out config)) |
|
|
|
else |
|
|
|
{ |
|
|
|
_logger.LogWarning($"客户端配置不存在: {address}"); |
|
|
|
return false; |
|
|
@ -308,8 +268,7 @@ namespace LTEMvcApp.Services |
|
|
|
/// <returns>客户端配置</returns>
|
|
|
|
public ClientConfig? GetClientConfig(string clientName) |
|
|
|
{ |
|
|
|
_configs.TryGetValue(clientName, out var config); |
|
|
|
return config; |
|
|
|
return _testClientConfigs.FirstOrDefault(c => c.Name == clientName); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -318,7 +277,7 @@ namespace LTEMvcApp.Services |
|
|
|
/// <returns>客户端配置列表</returns>
|
|
|
|
public List<ClientConfig> GetAllClientConfigs() |
|
|
|
{ |
|
|
|
return _configs.Values.ToList(); |
|
|
|
return _testClientConfigs.ToList(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -432,7 +391,7 @@ namespace LTEMvcApp.Services |
|
|
|
/// </summary>
|
|
|
|
public void StartAllConfiguredClients() |
|
|
|
{ |
|
|
|
foreach (var config in _configs.Values) |
|
|
|
foreach (var config in _testClientConfigs) |
|
|
|
{ |
|
|
|
if (config.Enabled) |
|
|
|
{ |
|
|
|