A:提供公共的网络链接
This commit is contained in:
1 parent
a87d58d76c
commit
c814da7056
6 files changed
+64
-32
No files matched your search
@@ -0,0 +1,5 @@
|
||||
namespace Common.Net;
|
||||
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Fleck" Version="1.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Common\Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,11 +1,6 @@
|
||||
namespace Application.Connections;
|
||||
using Common.Logger;
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.Json;
|
||||
using Fleck;
|
||||
using System.Timers;
|
||||
namespace Common.Net.WebSocketConn;
|
||||
|
||||
// 主程序示例
|
||||
public class Program
|
||||
@@ -23,7 +18,7 @@ public class Program
|
||||
// 注册自定义处理器
|
||||
wsManager.RegisterHandler("customCommand", async (cmd, socket) =>
|
||||
{
|
||||
Console.WriteLine($"处理自定义命令: {cmd.Command}");
|
||||
Log.Information($"处理自定义命令: {cmd.Command}");
|
||||
await wsManager.SendToClient(socket.ConnectionInfo.Id, new ResponseMessage
|
||||
{
|
||||
Type = "customResponse",
|
||||
@@ -34,7 +29,7 @@ public class Program
|
||||
// 启动网络监控
|
||||
networkMonitor.StartMonitoring(3000);
|
||||
|
||||
Console.WriteLine("服务器运行中,按任意键退出...");
|
||||
Log.Information("服务器运行中,按任意键退出...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
finally
|
||||
@@ -1,10 +1,12 @@
|
||||
namespace Application.Connections;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Common.Net.WebSocketConn;
|
||||
|
||||
// 消息基类
|
||||
public abstract class BaseMessage
|
||||
{
|
||||
public string MessageId { get; set; } = Guid.NewGuid().ToString();
|
||||
public DateTime Timestamp { get; set; } = DateTime.Now;
|
||||
[JsonPropertyName("messageId")] public string? MessageId { get; set; } = Guid.NewGuid().ToString();
|
||||
[JsonPropertyName("timestamp")] public DateTime Timestamp { get; set; } = DateTime.Now;
|
||||
}
|
||||
|
||||
// 网络状态消息
|
||||
@@ -30,22 +32,23 @@ public class FirewallRuleMessage : NotificationMessage
|
||||
// 命令消息
|
||||
public class CommandMessage : BaseMessage
|
||||
{
|
||||
public string Command { get; set; }
|
||||
[JsonPropertyName("command")] public string Command { get; set; }
|
||||
|
||||
public object Data { get; set; }
|
||||
[JsonPropertyName("data")] public object Data { get; set; }
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
public class ResponseMessage : NotificationMessage
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
[JsonPropertyName("success")] public bool Success { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通知消息 【服务端发往客户端的消息】
|
||||
/// </summary>
|
||||
public class NotificationMessage : BaseMessage
|
||||
{
|
||||
public string Type { get; set; } // 通知类型,如 "networkStatusUpdate", "firewallRuleChanged"
|
||||
public object Data { get; set; }
|
||||
public string Message { get; set; }
|
||||
[JsonPropertyName("type")] public string Type { get; set; } // 通知类型,如 "networkStatusUpdate", "firewallRuleChanged"
|
||||
[JsonPropertyName("data")] public object Data { get; set; }
|
||||
[JsonPropertyName("message")] public string Message { get; set; }
|
||||
}
|
||||
+5
-3
@@ -1,4 +1,6 @@
|
||||
namespace Application.Connections;
|
||||
using Common.Logger;
|
||||
|
||||
namespace Common.Net.WebSocketConn;
|
||||
|
||||
public class NetworkMonitorService
|
||||
{
|
||||
@@ -17,7 +19,7 @@ public class NetworkMonitorService
|
||||
_monitorTimer.Elapsed += async (sender, e) => await SendNetworkUpdate();
|
||||
_monitorTimer.AutoReset = true;
|
||||
_monitorTimer.Start();
|
||||
Console.WriteLine($"网络监控已启动,更新间隔: {intervalMs}ms");
|
||||
Log.Information($"网络监控已启动,更新间隔: {intervalMs}ms");
|
||||
}
|
||||
|
||||
// 停止监控
|
||||
@@ -25,7 +27,7 @@ public class NetworkMonitorService
|
||||
{
|
||||
_monitorTimer?.Stop();
|
||||
_monitorTimer?.Dispose();
|
||||
Console.WriteLine("网络监控已停止");
|
||||
Log.Information("网络监控已停止");
|
||||
}
|
||||
|
||||
// 发送网络状态更新
|
||||
+21
-11
@@ -1,8 +1,10 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using Common.Logger;
|
||||
using Fleck;
|
||||
|
||||
namespace Application.Connections;
|
||||
namespace Common.Net.WebSocketConn;
|
||||
|
||||
// WebSocket管理器 - 单例模式
|
||||
public class WebSocketManager
|
||||
@@ -41,7 +43,7 @@ public class WebSocketManager
|
||||
});
|
||||
|
||||
_isStarted = true;
|
||||
Console.WriteLine($"WebSocket服务器已启动: {url}");
|
||||
Log.Information($"WebSocket服务器已启动: {url}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,19 +53,26 @@ public class WebSocketManager
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (!_isStarted) return;
|
||||
|
||||
AppConfig.Instance.WebSocketPath = "";
|
||||
_server?.Dispose();
|
||||
CloseAllSocketsAsync();
|
||||
_connections.Clear();
|
||||
_isStarted = false;
|
||||
Console.WriteLine("WebSocket服务器已停止");
|
||||
Log.Information("WebSocket服务器已停止");
|
||||
}
|
||||
}
|
||||
public async Task CloseAllSocketsAsync()
|
||||
{
|
||||
foreach (var webSocketConnection in _connections)
|
||||
{
|
||||
webSocketConnection.Value.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// 连接建立
|
||||
private void OnConnectionOpen(IWebSocketConnection socket)
|
||||
{
|
||||
_connections.TryAdd(socket.ConnectionInfo.Id, socket);
|
||||
Console.WriteLine($"客户端连接: {socket.ConnectionInfo.Id}");
|
||||
Log.Information($"客户端连接: {socket.ConnectionInfo.Id}");
|
||||
|
||||
// 发送欢迎消息
|
||||
_ = SendToClient(socket.ConnectionInfo.Id, new ResponseMessage
|
||||
@@ -79,7 +88,7 @@ public class WebSocketManager
|
||||
private void OnConnectionClose(IWebSocketConnection socket)
|
||||
{
|
||||
_connections.TryRemove(socket.ConnectionInfo.Id, out _);
|
||||
Console.WriteLine($"客户端断开: {socket.ConnectionInfo.Id}");
|
||||
Log.Information($"客户端断开: {socket.ConnectionInfo.Id}");
|
||||
}
|
||||
|
||||
// 接收消息
|
||||
@@ -87,6 +96,7 @@ public class WebSocketManager
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine(DateTime.Now);
|
||||
var commandMessage = JsonSerializer.Deserialize<CommandMessage>(message);
|
||||
if (commandMessage != null && !string.IsNullOrEmpty(commandMessage.Command))
|
||||
{
|
||||
@@ -95,7 +105,7 @@ public class WebSocketManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"消息处理错误: {ex.Message}");
|
||||
Log.Information($"消息处理错误: {ex.Message}");
|
||||
await SendErrorResponse(socket, "消息格式错误");
|
||||
}
|
||||
}
|
||||
@@ -200,7 +210,7 @@ public class WebSocketManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"发送消息失败: {ex.Message}");
|
||||
Log.Information($"发送消息失败: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -225,7 +235,7 @@ public class WebSocketManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"广播消息失败: {ex.Message}");
|
||||
Log.Information($"广播消息失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,7 +295,7 @@ public class WebSocketManager
|
||||
private bool AddFirewallRule(FirewallRuleMessage rule)
|
||||
{
|
||||
// 实现添加防火墙规则逻辑
|
||||
Console.WriteLine($"添加防火墙规则: {rule.RuleName}");
|
||||
Log.Information($"添加防火墙规则: {rule.RuleName}");
|
||||
return true; // 示例返回
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user