使用MQTTnet开发MQTT服务-.NET Framework版本

        前面使用的RabbitMQ 的MQTT服务,但我们使用MQTTnet 也是可以自己开发出一个服务端的

GitHub - harrylsp/MQTTServer: MQTTServerMQTTServer. Contribute to harrylsp/MQTTServer development by creating an account on GitHub.https://github.com/harrylsp/MQTTServer

1.安装MQTTnet库

2.创建MQTT服务 

2.1 创建服务对象

public MqttServer mqttServer = null;
mqttServer = new MqttFactory().CreateMqttServer() as MqttServer;

2.2 创建服务对象参数配置

var optionsBuilder = new MqttServerOptionsBuilder()
.WithDefaultEndpoint().WithDefaultEndpointPort(1883).WithConnectionValidator(c =>
{
	var currentUser = username;
	var currentPwd = password;

	if (currentUser == null || currentPwd == null)
	{
		c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
		return;
	}

	if (c.Username != currentUser || c.Password != currentPwd)
	{
		c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
	}

	c.ReasonCode = MqttConnectReasonCode.Success;
})
.WithSubscriptionInterceptor(c =>
{
	c.AcceptSubscription = true;
})
.WithApplicationMessageInterceptor(c =>
{
	c.AcceptPublish = true;
});

2.3 注册监听回调事件

mqttServer.StartedHandler = new MqttServerStartedHandlerDelegate(new Action<EventArgs>(e =>
{
	if (mqttServer.IsStarted)
	{ }
	LogManager.WriteLogEx(LOGLEVEL.INFO, "MQTT服务启动完成!");
}));

mqttServer.StoppedHandler = new MqttServerStoppedHandlerDelegate(new Action<EventArgs>(e =>
{
	if (!mqttServer.IsStarted)
	{ }

	LogManager.WriteLogEx(LOGLEVEL.INFO, "MQTT服务停止完成!");
}));

mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(new Action<MqttServerClientConnectedEventArgs>(e =>
{
	LogManager.WriteLogEx(LOGLEVEL.INFO, $"客户端[{e.ClientId}]已连接");
}));

mqttServer.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(new Action<MqttServerClientDisconnectedEventArgs>(e =>
{
	LogManager.WriteLogEx(LOGLEVEL.INFO, $"客户端[{e.ClientId}]已断开连接!");
}));

mqttServer.ClientSubscribedTopicHandler = new MqttServerClientSubscribedTopicHandlerDelegate(new Action<MqttServerClientSubscribedTopicEventArgs>(e =>
{
	LogManager.WriteLogEx(LOGLEVEL.INFO, $"客户端[{e.ClientId}]已成功订阅主题[{e.TopicFilter}]!");
}));

mqttServer.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(new Action<MqttServerClientUnsubscribedTopicEventArgs>(e =>
{
	LogManager.WriteLogEx(LOGLEVEL.INFO, $"客户端[{e.ClientId}]已成功取消订阅主题[{e.TopicFilter}]!");
}));

mqttServer.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(new Action<MqttApplicationMessageReceivedEventArgs>(e =>
{
	LogManager.WriteLogEx(LOGLEVEL.INFO, $"客户端[{e.ClientId}]>> 主题:{e.ApplicationMessage.Topic} 负荷:{Encoding.UTF8.GetString(e.ApplicationMessage.Payload)} Qos:{e.ApplicationMessage.QualityOfServiceLevel} 保留:{e.ApplicationMessage.Retain}");
}));

2.4 开始启动服务对象

await mqttServer.StartAsync(optionsBuilder.Build());

2.5 服务运行

2.6 测试连接 

3. 发布消息

var message = new MqttApplicationMessage()
{
	Topic = topic,
	Payload = Encoding.UTF8.GetBytes(payload)
};

await mqttServer.PublishAsync(message);

 4. 其他

源代码:GitHub - harrylsp/MQTTServer: MQTTServer

更专业的MQTT服务 EMQX

https://www.emqx.com/zh/downloads-and-install?product=broker&version=4.3.10&os=Windows&oslabel=Windows

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值