visual basic 6.0中使用MQTT控件连接MQTT服务。
调用简单,支持MQTT5,ocx控件直接调用,支持utf-8、gbk编码,使用MQTT版本可选(mqtt3.1 , mqtt3.1.1 ,mqtt5)
VB6 MQTT客户端下载
MQTTClient 下载
部分调用代码
TMqtt1.KeepAlive = 180 '心跳包间隔时间 TMqtt1.MqttVersion = 4 'mqtt版本 支持 3(3.1),4(3.1.1),5 TMqtt1.DataEncode = "utf-8" 'utf-8,gbk cRet = TMqtt1.Connect("ip:port", "clientid", "用户名", "密码")
订阅
Dim ret As Long ret = TMqtt1.subscribe("订阅主题", 2) '订阅主题,QOS If ret = 0 Then Do While (TMqtt1.SUBSCRIBE_STAT = -1) Sleep (10) Loop If TMqtt1.SUBSCRIBE_STAT = 0 Then addListMsg "订阅主题[" & Text1.Text & "] 成功!" ElseIf TMqtt1.SUBSCRIBE_STAT = 1 Then addListMsg "订阅主题[" & Text1.Text & "] 失败!" End If End If
接收
Private Sub TMqtt1_onMessage(ByVal topic As String, ByVal payload As String, ByVal cid As String) addListMsg "接收消息[Topic:" & topic & "][PayLoad:" & payload & "]" End Sub
发布
If TMqtt1.sendMessage("发送主题", "发送内容", 2) = 0 Then addListMsg "发送消息[Topic:" & tTopicSend.Text & "][PayLoad:" & txtMsg.Text & "]" End If