A:WS 连接

This commit is contained in:
jimmy committed 2025-06-05 07:13:37 +08:00
1 parent be3411d4b5
commit b920614317
3 files changed
-76

No files matched your search

-5
View File
@@ -1,5 +0,0 @@
namespace Common.Net;
public class Class1
{
}
-4
View File
@@ -8,7 +8,6 @@ public class Program
public static void Main(string[] args)
{
var wsManager = WebSocketManager.Instance;
var networkMonitor = new NetworkMonitorService();
try
{
@@ -26,15 +25,12 @@ public class Program
Message = "自定义命令处理完成"
});
});
// 启动网络监控
networkMonitor.StartMonitoring(3000);
Log.Information("服务器运行中,按任意键退出...");
Console.ReadKey();
}
finally
{
networkMonitor.StopMonitoring();
wsManager.Stop();
}
}
@@ -1,67 +0,0 @@
using Common.Logger;
namespace Common.Net.WebSocketConn;
public class NetworkMonitorService
{
private System.Timers.Timer _monitorTimer;
private readonly WebSocketManager _wsManager;
public NetworkMonitorService()
{
_wsManager = WebSocketManager.Instance;
}
// 启动监控
public void StartMonitoring(int intervalMs = 5000)
{
_monitorTimer = new System.Timers.Timer(intervalMs);
_monitorTimer.Elapsed += async (sender, e) => await SendNetworkUpdate();
_monitorTimer.AutoReset = true;
_monitorTimer.Start();
Log.Information($"网络监控已启动,更新间隔: {intervalMs}ms");
}
// 停止监控
public void StopMonitoring()
{
_monitorTimer?.Stop();
_monitorTimer?.Dispose();
Log.Information("网络监控已停止");
}
// 发送网络状态更新
private async Task SendNetworkUpdate()
{
if (_wsManager.GetConnectionCount() > 0)
{
var networkData = GetCurrentNetworkStatus();
await _wsManager.BroadcastMessage(new ResponseMessage
{
Type = "networkStatusUpdate",
Success = true,
Data = networkData
});
}
}
private object GetCurrentNetworkStatus()
{
// 实现实时网络状态获取
return new
{
timestamp = DateTime.Now,
interfaces = new[]
{
new
{
name = "以太网",
status = "已连接",
speed = "1000 Mbps",
bytesSent = new Random().Next(1000000, 5000000),
bytesReceived = new Random().Next(2000000, 8000000)
}
}
};
}
}