From 78d9af6e5a6e322496b04bc758f3194c71bd3fd3 Mon Sep 17 00:00:00 2001 From: zuohuaijun Date: Thu, 18 Sep 2025 00:20:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8E=E5=8D=87=E7=BA=A7=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E8=B0=83=E6=95=B4=E6=96=87=E4=BB=B6=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin.NET.Core/Admin.NET.Core.csproj | 6 +- .../Admin.NET.Core/Job/MqttHostedService.cs | 200 +++++++++--------- Admin.NET/Admin.NET.sln | 4 +- Web/package.json | 8 +- 4 files changed, 109 insertions(+), 109 deletions(-) diff --git a/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj b/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj index e8e093b2..95368d49 100644 --- a/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj +++ b/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj @@ -22,7 +22,7 @@ - + @@ -40,7 +40,7 @@ - + @@ -52,7 +52,7 @@ - + diff --git a/Admin.NET/Admin.NET.Core/Job/MqttHostedService.cs b/Admin.NET/Admin.NET.Core/Job/MqttHostedService.cs index 63bafc5e..19a100bc 100644 --- a/Admin.NET/Admin.NET.Core/Job/MqttHostedService.cs +++ b/Admin.NET/Admin.NET.Core/Job/MqttHostedService.cs @@ -1,13 +1,13 @@ -// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 +// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! -namespace Admin.NET.Core; - +namespace Admin.NET.Core; + using Microsoft.Extensions.Hosting; -using MQTTnet; +using MQTTnet; using MQTTnet.Protocol; using MQTTnet.Server; using System; @@ -19,34 +19,34 @@ using System.Threading.Tasks; /// MQTT 服务 /// public class MqttHostedService(IOptions mqttOptions) : IHostedService, ISingleton -{ +{ private readonly MqttOptions _mqttOptions = mqttOptions.Value; - public static MqttServer MqttServer { get; set; } - public static readonly List MqttEventInterceptors = []; // MQTT 事件拦截器集合 - - /// - /// 注册 MQTT 事件拦截器 - /// - /// - /// - public static void AddMqttEventInterceptor(MqttEventInterceptor mqttEventInterceptor, int order = 0) - { - mqttEventInterceptor.Order = order; - MqttEventInterceptors.Add(mqttEventInterceptor); - MqttEventInterceptors.Sort((a, b) => b.Order - a.Order); - } - + public static MqttServer MqttServer { get; set; } + public static readonly List MqttEventInterceptors = []; // MQTT 事件拦截器集合 + + /// + /// 注册 MQTT 事件拦截器 + /// + /// + /// + public static void AddMqttEventInterceptor(MqttEventInterceptor mqttEventInterceptor, int order = 0) + { + mqttEventInterceptor.Order = order; + MqttEventInterceptors.Add(mqttEventInterceptor); + MqttEventInterceptors.Sort((a, b) => b.Order - a.Order); + } + public async Task StartAsync(CancellationToken cancellationToken) { - if (!_mqttOptions.Enabled) return; - - // 注册 MQTT 自定义客户端验证事件拦截器 - AddMqttEventInterceptor(new DefaultMqttEventInterceptor()); + if (!_mqttOptions.Enabled) return; + + // 注册 MQTT 自定义客户端验证事件拦截器 + AddMqttEventInterceptor(new DefaultMqttEventInterceptor()); var options = new MqttServerOptionsBuilder() .WithDefaultEndpoint() // 默认地址127.0.0.1 - .WithDefaultEndpointPort(_mqttOptions.Port) // 端口号 - //.WithDefaultEndpointBoundIPAddress(_mqttOptions.IPAddress) // IP地址 + .WithDefaultEndpointPort(_mqttOptions.Port) // 端口号 + //.WithDefaultEndpointBoundIPAddress(_mqttOptions.IPAddress) // IP地址 .WithConnectionBacklog(_mqttOptions.ConnectionBacklog) // 最大连接数 .WithPersistentSessions() .Build(); @@ -85,10 +85,10 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi /// private async Task MqttServer_StoppedAsync(EventArgs arg) { - Console.WriteLine($"【MQTT】服务已关闭...... {DateTime.Now}"); - foreach (var eh in MqttEventInterceptors) - { - await eh.StoppedAsync(arg); + Console.WriteLine($"【MQTT】服务已关闭...... {DateTime.Now}"); + foreach (var eh in MqttEventInterceptors) + { + await eh.StoppedAsync(arg); } } @@ -99,11 +99,11 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi /// private async Task MqttServer_ValidatingConnectionAsync(ValidatingConnectionEventArgs arg) { - foreach (var eh in MqttEventInterceptors) - { - await eh.ValidatingConnectionAsync(arg); - if (arg.ReasonCode != MqttConnectReasonCode.Success) - break; + foreach (var eh in MqttEventInterceptors) + { + await eh.ValidatingConnectionAsync(arg); + if (arg.ReasonCode != MqttConnectReasonCode.Success) + break; } } @@ -113,12 +113,12 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi /// /// private async Task MqttServer_ClientConnectedAsync(ClientConnectedEventArgs arg) - { - foreach (var eh in MqttEventInterceptors) - { - await eh.ClientConnectedAsync(arg); - } - + { + foreach (var eh in MqttEventInterceptors) + { + await eh.ClientConnectedAsync(arg); + } + Logging($"客户端连接:客户端ID=【{arg.ClientId}】已连接:用户名=【{arg.UserName}】地址=【{arg.RemoteEndPoint}】 {DateTime.Now}"); } @@ -130,11 +130,11 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi /// private async Task MqttServer_ClientDisconnectedAsync(ClientDisconnectedEventArgs arg) { - foreach (var eh in MqttEventInterceptors) - { - await eh.ClientDisconnectedAsync(arg); - } - + foreach (var eh in MqttEventInterceptors) + { + await eh.ClientDisconnectedAsync(arg); + } + Logging($"客户端断开:客户端ID=【{arg.ClientId}】已断开:用户名=【{arg.UserName}】地址=【{arg.RemoteEndPoint}】 {DateTime.Now}"); } @@ -145,11 +145,11 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi /// private async Task MqttServer_ClientSubscribedTopicAsync(ClientSubscribedTopicEventArgs arg) { - foreach (var eh in MqttEventInterceptors) - { - await eh.ClientSubscribedTopicAsync(arg); - } - + foreach (var eh in MqttEventInterceptors) + { + await eh.ClientSubscribedTopicAsync(arg); + } + Logging($"订阅主题:客户端ID=【{arg.ClientId}】订阅主题=【{arg.TopicFilter}】 {DateTime.Now}"); } @@ -160,11 +160,11 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi /// private async Task MqttServer_ClientUnsubscribedTopicAsync(ClientUnsubscribedTopicEventArgs arg) { - foreach (var eh in MqttEventInterceptors) - { - await eh.ClientUnsubscribedTopicAsync(arg); - } - + foreach (var eh in MqttEventInterceptors) + { + await eh.ClientUnsubscribedTopicAsync(arg); + } + Logging($"取消订阅:客户端ID=【{arg.ClientId}】取消订阅主题=【{arg.TopicFilter}】 {DateTime.Now}"); } @@ -178,11 +178,11 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi if (string.Equals(arg.ClientId, _mqttOptions.MqttServerId)) return; - foreach (var eh in MqttEventInterceptors) - { - await eh.InterceptingPublishAsync(arg); - } - + foreach (var eh in MqttEventInterceptors) + { + await eh.InterceptingPublishAsync(arg); + } + Logging($"拦截消息:客户端ID=【{arg.ClientId}】 Topic主题=【{arg.ApplicationMessage.Topic}】 消息=【{Encoding.UTF8.GetString(arg.ApplicationMessage.Payload)}】 qos等级=【{arg.ApplicationMessage.QualityOfServiceLevel}】 {DateTime.Now}"); } @@ -192,49 +192,49 @@ public class MqttHostedService(IOptions mqttOptions) : IHostedServi /// /// private async Task MqttServer_ApplicationMessageNotConsumedAsync(ApplicationMessageNotConsumedEventArgs arg) - { - foreach (var eh in MqttEventInterceptors) - { - await eh.ApplicationMessageNotConsumedAsync(arg); - } - - Logging($"接收消息:发送端ID=【{arg.SenderId}】 Topic主题=【{arg.ApplicationMessage.Topic}】 消息=【{Encoding.UTF8.GetString(arg.ApplicationMessage.Payload)}】 qos等级=【{arg.ApplicationMessage.QualityOfServiceLevel}】 {DateTime.Now}"); - } - - /// - /// 发布主题消息 - /// - /// - /// - public async Task PublicMessageAsync(string topic, string message) - { - var applicationMessage = new MqttApplicationMessageBuilder() - .WithTopic(topic) - .WithPayload(message) - .Build(); - - await MqttServer.InjectApplicationMessage(new InjectedMqttApplicationMessage(applicationMessage) - { - SenderClientId = _mqttOptions.MqttServerId, - SenderUserName = _mqttOptions.MqttServerId, - }); - - Logging($"服务器发布主题:{topic}, 内容:{message}"); + { + foreach (var eh in MqttEventInterceptors) + { + await eh.ApplicationMessageNotConsumedAsync(arg); + } + + Logging($"接收消息:发送端ID=【{arg.SenderId}】 Topic主题=【{arg.ApplicationMessage.Topic}】 消息=【{Encoding.UTF8.GetString(arg.ApplicationMessage.Payload)}】 qos等级=【{arg.ApplicationMessage.QualityOfServiceLevel}】 {DateTime.Now}"); + } + + /// + /// 发布主题消息 + /// + /// + /// + public async Task PublicMessageAsync(string topic, string message) + { + var applicationMessage = new MqttApplicationMessageBuilder() + .WithTopic(topic) + .WithPayload(message) + .Build(); + + await MqttServer.InjectApplicationMessage(new InjectedMqttApplicationMessage(applicationMessage) + { + SenderClientId = _mqttOptions.MqttServerId, + SenderUserName = _mqttOptions.MqttServerId, + }); + + Logging($"服务器发布主题:{topic}, 内容:{message}"); } public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; - } - - /// - /// 输出日志 - /// - /// - protected void Logging(string msg) - { - if (!_mqttOptions.Logging) return; - Console.WriteLine(msg); - Log.Information(msg); + } + + /// + /// 输出日志 + /// + /// + protected void Logging(string msg) + { + if (!_mqttOptions.Logging) return; + Console.WriteLine(msg); + Log.Information(msg); } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.sln b/Admin.NET/Admin.NET.sln index 5adf7a13..4b54f6bf 100644 --- a/Admin.NET/Admin.NET.sln +++ b/Admin.NET/Admin.NET.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.14.36429.23 +# Visual Studio Version 18 +VisualStudioVersion = 18.0.11010.61 d18.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Admin.NET.Application", "Admin.NET.Application\Admin.NET.Application.csproj", "{C3F5AEC5-ACEE-4109-94E3-3F981DC18268}" EndProject diff --git a/Web/package.json b/Web/package.json index efbf2faf..a22df87a 100644 --- a/Web/package.json +++ b/Web/package.json @@ -2,7 +2,7 @@ "name": "admin.net.pro", "type": "module", "version": "2.4.33", - "lastBuildTime": "2025.09.16", + "lastBuildTime": "2025.09.17", "description": "Admin.NET 站在巨人肩膀上的 .NET 通用权限开发框架", "author": "zuohuaijun", "license": "MIT", @@ -81,8 +81,8 @@ "vue-router": "^4.5.1", "vue-signature-pad": "^3.0.2", "vue3-tree-org": "^4.2.2", - "vxe-pc-ui": "^4.9.30", - "vxe-table": "^4.16.12", + "vxe-pc-ui": "^4.9.31", + "vxe-table": "^4.16.13", "xe-utils": "^3.7.9", "xlsx-js-style": "^1.2.0" }, @@ -90,7 +90,7 @@ "@iconify/vue": "^5.0.0", "@plugin-web-update-notification/vite": "^2.0.2", "@types/lodash-es": "^4.17.12", - "@types/node": "^22.18.4", + "@types/node": "^22.18.5", "@types/nprogress": "^0.2.3", "@types/sortablejs": "^1.15.8", "@typescript-eslint/eslint-plugin": "^8.44.0",