MQTT是一个基于客户端-服务器的消息发布/订阅传输协议,在物联网领域应用特别广泛。MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。在很多情况下,包括受限的环境中,如:机器与机器(M2M)通信和物联网(IoT)。其在,通过卫星链路通信传感器、偶尔拨号的医疗设备、智能家居、及一些小型化设备中已广泛使用。
来自于github高分的开源m2mqtt库
dll下载:M2Mqtt.Net.dll
github源码地址:https://github.com/eclipse/paho.mqtt.m2mqtt
空间名 | 别名 |
uPLibrary.Networking.M2Mqtt | mq |
uPLibrary.Networking.M2Mqtt.Messages | mqs |
重启项目,再检查命名空间是否还在。因为如果你写错英文,它会消失的
Public MqttSub As mq.MqttClient 'MQTT的1个订阅端,方便连接后全局调用 Public MqttPub As mq.MqttClient 'MQTT的1个发布端,方便连接后全局调用 Public MqttQosDic As new Dictionary(of Integer,Byte)'qos质量枚举,在AfterOpenProject后初始化 '1订阅者相关--- 'MQTT订阅者断开连接后触发的事件 Public Sub SubConnectionClosed(ByVal sender As Object, ByVal e As EventArgs) Functions.Execute("MqttSubDisConnected") End Sub 'MQTT订阅者订阅后触发的事件 Public Sub MqttMsgSubscribed(ByVal sender As Object, ByVal e As mqs.MqttMsgSubscribedEventArgs) Functions.Execute("MqttMsgSubscribed") End Sub 'MQTT订阅者收到消息时触发的事件 Public Sub MqttMsgPublishReceived(ByVal sender As Object, ByVal e As mqs.MqttMsgPublishEventArgs) Functions.Execute("MqttReceivedMessage",e.Message,e.Topic,e.QosLevel) End Sub 'MQTT订阅者取消订阅后触发的事件 Public Sub MqttMsgUnsubscribed(ByVal sender As Object, ByVal e As mqs.MqttMsgUnsubscribedEventArgs) Functions.Execute("MqttMsgUnsubscribed") End Sub '2发布者相关 'MQTT发布者断开连接后触发的事件 Public Sub PubConnectionClosed(ByVal sender As Object, ByVal e As EventArgs) Functions.Execute("MqttPubDisConnected") End Sub
'初始化MqttQos的枚举--- MqttQosDic.Add(0,mqs.MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE) '0级,最多1次,消息可能会丢失 MqttQosDic.Add(1,mqs.MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE) '1级,最少1次,消息可能会重复 MqttQosDic.Add(2,mqs.MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE) '2级,仅发1次,会增加开销甚至堵塞 '初始化MqttQos的枚举---
内部函数名:MqttMsgSubscribed
'MQTT订阅者订阅主题后触发的委托事件
MqttMsgUnsubscribed
'MQTT订阅者订阅后触发的委托事件
内部函数名:MqttReceivedMessage
'订阅的MQTT收到信息时触发的事件 '注意编码有Default和UTF8,乱码请切换 Dim msg As String = Encoding.Default.GetString(args(0)) '消息内容 Dim Topic As String = args(1) '主题名 Dim QosLevel As Integer = args(2) 'qos质量,有0,1,2 MqttSubDisconnected
'MQTT订阅者断开连接后触发的委托事件
MqttPubDisconnected
'MQTT发布者断开连接后的委托事件
MqttPublish
'对某个主题发送某个qos质量的消息 '通过 Functions.Execute("MqttPublish","主题","内容",0,False) 使用 'MqttQosDic是全局Qos枚举变量,已经定义好012级别 Dim Topic As String = Args(0) '主题 Dim msg As String = Args(1) '消息文本内容 Dim qos As Integer = Args(2) '质量等级,有012 Dim retain As Boolean = Args(3) 'Retain若为True,该条消息将被保留做为最后一条收到的消息。下一个订阅消息者将能至少收到该条消息 If MqttPub.IsConnected Then '注意编码类型有Default和UTF8,乱码请切换 MqttPub.Publish(Topic, Encoding.Default.GetBytes(msg), MqttQosDic(qos),retain) End If
我的help.mdb为狐表的代码精灵,增加了mqtt的一些提示
MqttSub = new mq.MqttClient("127.0.0.1",1884,False,new System.Security.Cryptography.X509Certificates.X509Certificate,new System.Security.Cryptography.X509Certificates.X509Certificate,new mq.MqttSslProtocols ) '或者new mq.MqttClient("http://域名:端口号/") '把全局代码里自定义的事件委托给当前MqttSub实例 AddHandler MqttSub.ConnectionClosed, AddressOf SubConnectionClosed AddHandler MqttSub.MqttMsgSubscribed, AddressOf MqttMsgSubscribed AddHandler MqttSub.MqttMsgPublishReceived , AddressOf MqttMsgPublishReceived AddHandler MqttSub.MqttMsgUnsubscribed, AddressOf MqttMsgUnsubscribed
If MqttSub.IsConnected =False Then MqttSub.Connect(Guid.NewGuid.ToString,"admin","111") '指定连接的随机ID,账号,密码 '如果没有账号密码 MqttSub.Connect(Guid.NewGuid.ToString) output.show(MqttSub.IsConnected) End If
支持多个主题,用数组传入
注意有多少个主题,就有多少个质量系数
MqttSub.Subscribe({"mytopic"},{MqttQosDic(0)})
MqttSub.UnSubscribe({"mytopic"})
If MqttPub.IsConnected Then MqttPub.Disconnect End If
MqttPub = new mq.MqttClient("127.0.0.1",1884,False,new System.Security.Cryptography.X509Certificates.X509Certificate,new System.Security.Cryptography.X509Certificates.X509Certificate,new mq.MqttSslProtocols ) '或者new mq.MqttClient("http://域名:端口号/") '把全局代码里自定义的事件委托给当前MqttSub实例 AddHandler MqttPub.ConnectionClosed, AddressOf PubConnectionClosed
If MqttPub.IsConnected =False Then MqttPub.Connect(Guid.NewGuid.ToString,"admin","111") '指定连接的随机ID,账号,密码 '如果没有账号密码 MqttSub.Connect(Guid.NewGuid.ToString) Output.show(MqttPub.IsConnected) End If
Functions.Execute("MqttPublish","主题","内容",0,False)
关闭后会释放内存,重连需要重新创建实例
If MqttPub.IsConnected Then MqttPub.Disconnect End If
这个dll对主流的MQTT服务器版本都支持,Windows下,我推荐mosquitto,免安装,直接cmd运行即可。使用方法自行百度
一个程序,可以同时对同一个mqtt服务器进行订阅和发布
一般是先有人订阅主题,再对主题发布信息。如果反过来,则发布的消息会被服务器当垃圾扔掉
不支持TLS1.2加密,因为.net4.0框架不支持