大家好,又见面了,我是你们的朋友全栈君。
C#MQTTNET服务端使用说明
- 本次演示使用到的库为:MQTTNET
- 开发环境:.NET Framework 4.5以上 或 .Net Core 3.1以上
- MQTTNET 其实不太吃.Net版本,这里为了演示用的**.Net Core 3.1**
- 因为MQTTNET 是一个标准库,这就意味着你.Net Core版本会使用了,.NET Framework 4.5
1.安装MQTTNet
两种方式
Shell
Install-Package MQTTNET
代码地址
可视化安装
- 鼠标右击你的项目——>
- 管理NuGet程序包
- 再NuGet包页面切换到浏览
- 搜索MQTTNet
2.当前我的项目安装的依赖如下
- MQTTnet 3.0.16 必要
- Microsoft.Extensions.Configuration 3.1.16
- Microsoft.Extensions.Configuration.Abstractions 3.1.16
- Microsoft.Extensions.Configuration.FileExtensions 3.1.16
- Microsoft.Extensions.Configuration.Json 3.1.16
- Newtonsoft.Json 13.0.1
- Microsoft.Extensions.Options.ConfigurationExtensions 3.1.16
3.如何使用
1.我想启动MQTT服务器,我该怎么做?
IMqttServer server = new MqttFactory().CreateMqttServer();
MqttServerOptionsBuilder serverOptions = new MqttServerOptionsBuilder();
serverOptions.WithDefaultEndpointPort(8080);
await server.StartAsync(serverOptions.Build());
2.我想知道MQTT服务器是否启动成功,我该怎么做?
server.StartedHandler = new MqttServerStartedHandlerDelegate(StartedHandler);
/// <summary>
/// MQTT启动服务器事件
/// </summary>
/// <param name="obj"></param>
private void StartedHandler(EventArgs obj)
{
Console.WriteLine($"程序已经启动!监听端口为:{model.Port}");
}
3.我想知道MQTT服务器是否停止允许,我该怎么做?
server.StoppedHandler = new MqttServerStoppedHandlerDelegate(StoppedHandler);
/// <summary>
/// MQTT服务器停止事件
/// </summary>
/// <param name="obj"></param>
private void StoppedHandler(EventArgs obj)
{
Console.WriteLine("程序已经关闭");
}
4.我想知道有那些设备连接上了服务器,我该怎么做?
//客户端连接事件
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(ClientConnectedHandler);
/// <summary>
/// 客户端连接到服务器事件
/// </summary>
/// <param name="obj"></param>
private void ClientConnectedHandler(MqttServerClientConnectedEventArgs obj)
{
throw new NotImplementedException();
}
server.UseClientConnectedHandler(args =>
{
Console.WriteLine($"{args.ClientId}此客户端已经连接到服务器");
});
5.我想知道有那些设备断开连接,我该怎么做?
//客户端断开连接事件
server.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(ClientDisconnectedHandler);
private void ClientDisconnectedHandler(MqttServerClientDisconnectedEventArgs obj)
{
Console.WriteLine($"断开连接的客户端:{obj.ClientId}");
Console.WriteLine($"断开连接类型:{obj.DisconnectType.ToString()}");
}
server.UseClientDisconnectedHandler(args => {
Console.WriteLine($"断开连接的客户端:{args.ClientId}");
Console.WriteLine($"断开连接类型:{args.DisconnectType.ToString()}");
});
6.我想监听消息,我该怎么做?
server.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(MessageReceivedHandler);
/// <summary>
/// 收到各个客户端发送的消息
/// </summary>
/// <param name="obj"></param>
private void MessageReceivedHandler(MqttApplicationMessageReceivedEventArgs obj)
{
Console.WriteLine("===================================================");
Console.WriteLine("收到消息:");
Console.WriteLine($"客户端:{obj.ClientId}");
Console.WriteLine($"主题:{obj.ApplicationMessage.Topic}");
Console.WriteLine($"消息:{Encoding.UTF8.GetString(obj.ApplicationMessage.Payload)}");
Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++");
Console.WriteLine();
}
server.UseApplicationMessageReceivedHandler(args=>{
Console.WriteLine("===================================================");
Console.WriteLine("收到消息:");
Console.WriteLine($"客户端:{args.ClientId}");
Console.WriteLine($"主题:{args.ApplicationMessage.Topic}");
Console.WriteLine($"消息:{Encoding.UTF8.GetString(args.ApplicationMessage.Payload)}");
Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++");
Console.WriteLine();
});
7.我想连接的客户端校验身份信息后才允许收发消息,我该怎么做?
IMqttServer server = new MqttFactory().CreateMqttServer();
MqttServerOptionsBuilder serverOptions = new MqttServerOptionsBuilder();
serverOptions.WithConnectionValidator(client => {
string Account = client.Username;
string PassWord = client.Password;
string clientid = client.ClientId;
if (Account == "test" && PassWord == "1234")
{
client.ReasonCode = MqttConnectReasonCode.Success;
Console.WriteLine("校验成功");
}
else
{
client.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
Console.WriteLine("校验失败");
}
});
await server.StartAsync(serverOptions.Build());
8.我想知道客户端订阅主题与取消订阅主题,我该怎么做?
1.订阅
//客户端订阅主题事件
server.ClientSubscribedTopicHandler = new MqttServerClientSubscribedHandlerDelegate(ClientSubscribedTopicHandler);
/// <summary>
/// 客户端订阅的主题
/// </summary>
/// <param name="obj"></param>
private void ClientSubscribedTopicHandler(MqttServerClientSubscribedTopicEventArgs obj)
{
Console.WriteLine($"客户端:{obj.ClientId}");
Console.WriteLine($"订阅主题:{obj.TopicFilter.Topic}");
}
2.取消订阅
//客户端取消订阅主题事件
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(ClientUnsubscribedTopicHandler);
/// <summary>
/// 客户端取消订阅主题
/// </summary>
/// <param name="obj"></param>
private void ClientUnsubscribedTopicHandler(MqttServerClientUnsubscribedTopicEventArgs obj)
{
Console.WriteLine($"客户端:{obj.ClientId}");
Console.WriteLine($"取消订阅主题:{obj.TopicFilter}");
}
9.我想关闭MQTT服务器,我该怎么做?
public async Task StopAsync()
{
if (server != null)
{
if (server.IsStarted)
{
await server.StopAsync();
server.Dispose();
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/152895.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...