From 7be44af3f1129f3ea69ae62f2335ff6b28117229 Mon Sep 17 00:00:00 2001 From: root <295172551@qq.com> Date: Thu, 26 Jun 2025 00:47:38 +0800 Subject: [PATCH] 12311 --- LTEMvcApp/Controllers/ClientController.cs | 30 +++---- LTEMvcApp/Controllers/HomeController.cs | 14 ++-- LTEMvcApp/Controllers/TestConfigController.cs | 4 +- LTEMvcApp/Services/WebSocketManagerService.cs | 78 ++++++++----------- 4 files changed, 56 insertions(+), 70 deletions(-) diff --git a/LTEMvcApp/Controllers/ClientController.cs b/LTEMvcApp/Controllers/ClientController.cs index 9591325..b46a488 100644 --- a/LTEMvcApp/Controllers/ClientController.cs +++ b/LTEMvcApp/Controllers/ClientController.cs @@ -36,38 +36,38 @@ namespace LTEMvcApp.Controllers /// /// 启动客户端 /// - /// 客户端名称 + /// 客户端地址 /// 操作结果 - [HttpPost("{clientName}/start")] - public ActionResult StartClient(string clientName) + [HttpPost("{address}/start")] + public ActionResult StartClient(string address) { - _logger.LogInformation($"API请求: 启动客户端 {clientName}"); - var success = _webSocketManager.StartClient(clientName); + _logger.LogInformation($"API请求: 启动客户端 {address}"); + var success = _webSocketManager.StartClient(address); if (success) { - _logger.LogInformation($"客户端 {clientName} 启动成功"); - return Ok(new { message = $"客户端 '{clientName}' 已启动" }); + _logger.LogInformation($"客户端 {address} 启动成功"); + return Ok(new { message = $"客户端 '{address}' 已启动" }); } else { - _logger.LogWarning($"客户端 {clientName} 启动失败"); - return BadRequest($"启动客户端 '{clientName}' 失败"); + _logger.LogWarning($"客户端 {address} 启动失败"); + return BadRequest($"启动客户端 '{address}' 失败"); } } /// /// 停止客户端 /// - /// 客户端名称 + /// 客户端地址 /// 操作结果 - [HttpPost("{clientName}/stop")] - public ActionResult StopClient(string clientName) + [HttpPost("{address}/stop")] + public ActionResult StopClient(string address) { - var success = _webSocketManager.StopClient(clientName); + var success = _webSocketManager.StopClient(address); if (success) - return Ok(new { message = $"客户端 '{clientName}' 已停止" }); + return Ok(new { message = $"客户端 '{address}' 已停止" }); else - return BadRequest($"停止客户端 '{clientName}' 失败"); + return BadRequest($"停止客户端 '{address}' 失败"); } /// diff --git a/LTEMvcApp/Controllers/HomeController.cs b/LTEMvcApp/Controllers/HomeController.cs index 2d826cc..29a2427 100644 --- a/LTEMvcApp/Controllers/HomeController.cs +++ b/LTEMvcApp/Controllers/HomeController.cs @@ -97,10 +97,10 @@ public class HomeController : Controller /// 启动测试客户端 /// [HttpPost] - public IActionResult StartTestClient() + public IActionResult StartTestClient(string address) { - _logger.LogInformation("启动测试客户端"); - var success = _webSocketManager.StartClient("测试客户端"); + _logger.LogInformation($"启动测试客户端: {address}"); + var success = _webSocketManager.StartClient(address); if (success) { TempData["Message"] = "测试客户端已启动"; @@ -109,7 +109,6 @@ public class HomeController : Controller { TempData["Error"] = "启动测试客户端失败"; } - return RedirectToAction(nameof(Index)); } @@ -117,10 +116,10 @@ public class HomeController : Controller /// 停止测试客户端 /// [HttpPost] - public IActionResult StopTestClient() + public IActionResult StopTestClient(string address) { - _logger.LogInformation("停止测试客户端"); - var success = _webSocketManager.StopClient("测试客户端"); + _logger.LogInformation($"停止测试客户端: {address}"); + var success = _webSocketManager.StopClient(address); if (success) { TempData["Message"] = "测试客户端已停止"; @@ -129,7 +128,6 @@ public class HomeController : Controller { TempData["Error"] = "停止测试客户端失败"; } - return RedirectToAction(nameof(Index)); } diff --git a/LTEMvcApp/Controllers/TestConfigController.cs b/LTEMvcApp/Controllers/TestConfigController.cs index c0d58e7..fac24d8 100644 --- a/LTEMvcApp/Controllers/TestConfigController.cs +++ b/LTEMvcApp/Controllers/TestConfigController.cs @@ -128,7 +128,7 @@ namespace LTEMvcApp.Controllers if (config == null) return NotFound($"未找到地址为 {address} 的测试客户端配置"); - var success = _webSocketManager.StartClient(config.Name); + var success = _webSocketManager.StartClient(config.Address); if (success) return Ok(new { message = $"测试客户端 {config.Name} 已启动" }); else @@ -164,7 +164,7 @@ namespace LTEMvcApp.Controllers return NotFound($"未找到地址为 {address} 的测试客户端配置"); _logger.LogInformation($"API 请求: 停止测试客户端 {config.Name}"); - var success = _webSocketManager.StopClient(config.Name); + var success = _webSocketManager.StopClient(config.Address); if (success) return Ok(new { message = $"测试客户端 {config.Name} 停止成功" }); else diff --git a/LTEMvcApp/Services/WebSocketManagerService.cs b/LTEMvcApp/Services/WebSocketManagerService.cs index da351a2..34d4e4c 100644 --- a/LTEMvcApp/Services/WebSocketManagerService.cs +++ b/LTEMvcApp/Services/WebSocketManagerService.cs @@ -162,65 +162,53 @@ namespace LTEMvcApp.Services /// /// 启动客户端 /// - /// 客户端名称 + /// 客户端地址 /// 是否成功启动 - public bool StartClient(string clientName) + public bool StartClient(string address) { - _logger.LogInformation($"启动客户端: {clientName}"); - + _logger.LogInformation($"启动客户端: {address}"); ClientConfig config; - // 检查是否是测试客户端 - var testConfig = _testClientConfigs.FirstOrDefault(c => c.Name == clientName); + var testConfig = _testClientConfigs.FirstOrDefault(c => c.Address == address); if (testConfig != null) { config = testConfig; - _logger.LogInformation($"使用测试客户端配置: {config.Name}"); + _logger.LogInformation($"使用测试客户端配置: {config.Address}"); } - else if (!_configs.TryGetValue(clientName, out config)) + else if (!_configs.TryGetValue(address, out config)) { - _logger.LogWarning($"客户端配置不存在: {clientName}"); + _logger.LogWarning($"客户端配置不存在: {address}"); return false; } - // 如果客户端已存在,先停止 - if (_clients.TryGetValue(clientName, out var existingClient)) + if (_clients.TryGetValue(address, out var existingClient)) { existingClient.Stop(); - _clients.TryRemove(clientName, out _); + _clients.TryRemove(address, out _); } - - // 通过依赖注入获取ILogger var logger = _serviceProvider.GetService(typeof(ILogger)) as ILogger; var client = new LTEClientWebSocket(config, logger!); - - // 订阅事件 client.ConnectionOpened += (sender, e) => OnClientConnected(client); client.ConnectionClosed += (sender, e) => OnClientDisconnected(client); - client.LogsReceived += (sender, logs) => OnLogsReceived(clientName, logs); - client.StateChanged += (sender, state) => OnStateChanged(clientName, state); - - // 启动客户端 + client.LogsReceived += (sender, logs) => OnLogsReceived(address, logs); + client.StateChanged += (sender, state) => OnStateChanged(address, state); client.Start(); - - // 添加到客户端列表 - _clients[clientName] = client; - + _clients[address] = client; return true; } /// /// 停止客户端 /// - /// 客户端名称 + /// 客户端地址 /// 是否成功停止 - public bool StopClient(string clientName) + public bool StopClient(string address) { - _logger.LogInformation($"停止客户端: {clientName}"); - if (_clients.TryGetValue(clientName, out var client)) + _logger.LogInformation($"停止客户端: {address}"); + if (_clients.TryGetValue(address, out var client)) { client.Stop(); - _clients.TryRemove(clientName, out _); + _clients.TryRemove(address, out _); return true; } return false; @@ -229,11 +217,11 @@ namespace LTEMvcApp.Services /// /// 获取客户端状态 /// - /// 客户端名称 + /// 客户端地址 /// 客户端状态 - public ClientState? GetClientState(string clientName) + public ClientState? GetClientState(string address) { - if (_clients.TryGetValue(clientName, out var client)) + if (_clients.TryGetValue(address, out var client)) { return client.State; } @@ -384,7 +372,7 @@ namespace LTEMvcApp.Services { if (config.Enabled) { - StartClient(config.Name); + StartClient(config.Address); } } } @@ -392,11 +380,11 @@ namespace LTEMvcApp.Services /// /// 获取客户端实例 /// - /// 客户端名称 + /// 客户端地址 /// 客户端实例 - public LTEClientWebSocket? GetClientInstance(string clientName) + public LTEClientWebSocket? GetClientInstance(string address) { - _clients.TryGetValue(clientName, out var client); + _clients.TryGetValue(address, out var client); return client; } @@ -476,8 +464,8 @@ namespace LTEMvcApp.Services public bool StartTestClient() { var defaultConfig = GetDefaultTestClientConfig(); - _logger.LogInformation("启动测试客户端: " + defaultConfig.Name); - return StartClient(defaultConfig.Name); + _logger.LogInformation("启动测试客户端: " + defaultConfig.Address); + return StartClient(defaultConfig.Address); } /// @@ -487,8 +475,8 @@ namespace LTEMvcApp.Services public bool StopTestClient() { var defaultConfig = GetDefaultTestClientConfig(); - _logger.LogInformation("停止测试客户端: " + defaultConfig.Name); - return StopClient(defaultConfig.Name); + _logger.LogInformation("停止测试客户端: " + defaultConfig.Address); + return StopClient(defaultConfig.Address); } /// @@ -498,7 +486,7 @@ namespace LTEMvcApp.Services public LTEClientWebSocket? GetTestClient() { var defaultConfig = GetDefaultTestClientConfig(); - return GetClientInstance(defaultConfig.Name); + return GetClientInstance(defaultConfig.Address); } /// @@ -511,7 +499,7 @@ namespace LTEMvcApp.Services foreach (var config in _testClientConfigs) { - if (_clients.TryGetValue(config.Name, out var client)) + if (_clients.TryGetValue(config.Address, out var client)) { testClients.Add(client); } @@ -530,7 +518,7 @@ namespace LTEMvcApp.Services foreach (var config in _testClientConfigs) { - var client = GetClientInstance(config.Name); + var client = GetClientInstance(config.Address); var state = client?.State ?? ClientState.Stop; result.Add(new { Config = config, State = state, Client = client }); @@ -735,7 +723,7 @@ namespace LTEMvcApp.Services /// private void OnClientConnected(LTEClientWebSocket client) { - _logger.LogInformation($"客户端已连接: {client.Client.Config.Name}"); + _logger.LogInformation($"客户端已连接: {client.Client.Config.Address}"); ClientConnected?.Invoke(this, client); } @@ -744,7 +732,7 @@ namespace LTEMvcApp.Services /// private void OnClientDisconnected(LTEClientWebSocket client) { - _logger.LogWarning($"客户端已断开: {client.Client.Config.Name}"); + _logger.LogWarning($"客户端已断开: {client.Client.Config.Address}"); ClientDisconnected?.Invoke(this, client); }