MQTTnet入门(二) - 验证来自MQTT客户端的连接请求
文章目录
前言
通过《MQTTnet入门(一) - 创建最简易的MQTT服务器》中提供的Demo已经可以实现一个简易的MQTT服务了,但在MQTTnet的Server上还有很多的配置和接口可以允许我们继续开发,本文就验证来自客户端的连接请求的问题提供一个可行的Demo。
验证连接请求的MQTT服务器Demo
在Client请求建立连接时,我们可以通过接口获得一个MqttConnectionValidatorContext类型的对象,通过它我们能获得当前建立连接请求的基本参数,例如:ClientId、Username 、Password 等。我们通过对这些参数的验证并依照验证结果修改ReasonCode 的状态值,即可实现我们的需求。
下面就是代码了:
using MQTTnet;
using MQTTnet.Protocol;
using MQTTnet.Server;
using System;
namespace MQTT
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// 创建MQTT服务配置的构建器
MqttServerOptionsBuilder optionsBuilder = new MqttServerOptionsBuilder();
// 为构建器添加连接验证的处理过程
optionsBuilder.WithConnectionValidator(context => {
// 验证ClientID的长度
if (context.ClientId.Length < 10)
{
context.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
return;
}
// 验证用户名和密码
if (context.Username != "user01" || context.Password != "123456")
{
context.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
return;
}
// 标记当前连接请求的ReasonCode为Success
context.ReasonCode = MqttConnectReasonCode.Success;
});
// 利用MqttFactory创建MQTT服务器
IMqttServer mqttServer = new MqttFactory().CreateMqttServer();
// 采用异步方式启动服务(使用服务配置构建器创建的对象)
await mqttServer.StartAsync(optionsBuilder.Build());
Console.WriteLine("按任意键退出程序...");
Console.ReadLine();
// 异步关闭MQTT服务
await mqttServer.StopAsync();
}
}
}
在代码中预设了一个用户名为user01,密码为123456的账户,可在此替换成其他的验证方式,这里就不再啰嗦了。至此,验证来自MQTT客户端的连接请求的功能就添加完成了。MQTTnet.Server的更多功能可以参考附录中的API。
附录:MQTTnet API - Server
属性
客户端建立连接时的处理过程
Description: The connected handler to perform actions when a client connected.
数据类型:IMqttServerClientConnectedHandler
属性名:ClientConnectedHandler
默认值:null
客户端断开连接时的处理过程
Description: The disconnected handler to perform actions when a client lost the connection.
数据类型:IMqttServerClientDisconnectedHandler
属性名:ClientDisconnectedHandler
默认值:null
客户端在订阅主题时的处理过程
Description: The subscribed handler to perform actions when a client subscribed.
数据类型:IMqttServerClientSubscribedTopicHandler
属性名:ClientSubscribedTopicHandler
默认值:null
客户端在退订主题时的处理过程
Description: The subscribed handler to perform actions when a client unsubscribed.
数据类型:IMqttServerClientUnsubscribedTopicHandler
属性名:ClientUnsubscribedTopicHandler
默认值:null
服务器配置项
Description: The server options set to the client.
数据类型:IMqttServerOptions
属性名:Options
默认值:null
服务器启动时的处理过程
Description: The started handler to perform actions when the server started.
数据类型:IMqttServerStartedHandler
属性名:StartedHandler
默认值:null
服务器关闭后的处理过程
Description: The stopped handler to perform actions when the server stopped.
数据类型:IMqttServerStoppedHandler
属性名:StoppedHandler
默认值:null
方法
清除保留的应用程序消息
Description: Clears the retained application messages.
返回类型:Task
方法名:ClearRetainedApplicationMessagesAsync()
获取客户端状态
Description: Gets the client status.
返回类型:Task
方法名:GetClientStatusAsync()
获取保留的应用程序消息
Description: Gets the retained application messages.
返回类型:Task
方法名:GetRetainedApplicationMessagesAsync()
获取会话状态
Description: Gets the session status.
返回类型:Task
方法名:GetSessionStatusAsync()
启动服务器
Description: Starts the server.
返回类型:Task
方法名:StartAsync(IMqttServerOptions options)
停止服务器
Description: Stops the server.
返回类型:Task
方法名:StopAsync()
在服务器上为客户端订阅主题
Description: Subscribes the server to topics.
返回类型:Task
方法名:SubscribeAsync(string clientId, ICollection topicFilters)
在服务器上为客户端退订主题
Description: Unsubscribes the server from topics
返回类型:Task
方法名:UnsubscribeAsync(string clientId, ICollection topicFilters)