91 lines
2.9 KiB
C#
91 lines
2.9 KiB
C#
|
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|||
|
|
//
|
|||
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|||
|
|
//
|
|||
|
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|||
|
|
|
|||
|
|
using MQTTnet.Server;
|
|||
|
|
using MQTTnet;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Runtime.InteropServices;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using MQTTnet.Protocol;
|
|||
|
|
using Furion.Logging.Extensions;
|
|||
|
|
using Furion.HttpRemote;
|
|||
|
|
|
|||
|
|
namespace Admin.NET.Core;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Mqtt事件拦截器
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>
|
|||
|
|
/// 为了让底层的 Mqtt 代码与业务层代码分离,应该不同的业务继函 MqttEventHandler 类,实现自己的代码后
|
|||
|
|
/// 在 Startup.cs 的 Configure 函数中,调用 MqttHostedService 对象的 RegistMqttEventHandler 函数,来注册自己的事件处理器
|
|||
|
|
/// “业务相关的代码千万不要写在 MqttHostedService 中”
|
|||
|
|
/// </remarks>
|
|||
|
|
|
|||
|
|
public class MqttEventHandler
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 排序,数据越大越先执行
|
|||
|
|
/// </summary>
|
|||
|
|
public int Order = 0;
|
|||
|
|
|
|||
|
|
protected void Log(string msg)
|
|||
|
|
{
|
|||
|
|
var mqttOptions = App.GetOptions<MqttOptions>();
|
|||
|
|
if (mqttOptions.ConsoleOutput)
|
|||
|
|
Console.WriteLine(msg);
|
|||
|
|
if (mqttOptions.Logging)
|
|||
|
|
msg.LogDebug();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public virtual async Task ValidatingConnectionAsync(ValidatingConnectionEventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public virtual async Task StartedAsync(EventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public virtual async Task StoppedAsync(EventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 客户端发布的数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="arg"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public virtual async Task InterceptingPublishAsync(InterceptingPublishEventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 没有客户端接收的数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="arg"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public virtual async Task ApplicationMessageNotConsumedAsync(ApplicationMessageNotConsumedEventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public virtual async Task ClientSubscribedTopicAsync(ClientSubscribedTopicEventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
public virtual async Task ClientUnsubscribedTopicAsync(ClientUnsubscribedTopicEventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public virtual async Task ClientDisconnectedAsync(ClientDisconnectedEventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public virtual async Task ClientConnectedAsync(ClientConnectedEventArgs arg)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
}
|