更新记录
1.0.1(2024-11-12)
- 修复高版本HB打包时,无证书连接问题
1.0.0(2024-10-11)
mqtt收发数据支持iOS和android
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:4.4,iOS:12,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
mqtt收发数据
支持mqtt 3.x、5.x版本
开发文档
- 集成插件,集成插件步骤请参考
https://www.cnblogs.com/wenrisheng/p/18323027
import {
UTSMqtt
} from "@/uni_modules/wrs-uts-mqtt"
let mqtt = new UTSMqtt()
mqtt.setCallback((resp) => {
this.showMsg(JSON.stringify(resp))
let opt = resp.opt
switch (opt) {
// 连接成功
case "didConnect":
break;
// 断开连接
case "didDisconnect":
break;
// 收到数据
case "didReceiveMessage":
let message = resp.message
let topic = resp.topic
mqtt.publish(this.sendTopic, {
qos: message.qos,
id: message.id,
retained: message.retained,
payload: message.payload
})
break;
default:
break;
}
})
let params = {}
// 是否使用SSL证书连接
var enableSSL = false
params.enableSSL = enableSSL
if(enableSSL) {
// ssl证书连接
params.host = "devops.xx.com"
params.port = 2607
params.username = "7E1a262F"
params.password = "e42A9611"
if (this.isAndroid) {
params.cer = {
ca: plus.io.convertLocalFileSystemURL("_www/static/ca.cer"),
cer: plus.io.convertLocalFileSystemURL("_www/static/client.cer"),
key: plus.io.convertLocalFileSystemURL("_www/static/client.key"),
password: "e42A9611"
}
} else {
params.cer = {
filePath: plus.io.convertLocalFileSystemURL("_www/static/mycert.p12"),
password: "e42A9611"
}
}
} else {
// 没有ssl证书连接
params.host = "xx"
params.port = 1883
params.username = "xx"
params.password = "xx"
}
params.clientID = "xxxaaabb"
params.allowUntrustCACertificate = true
mqtt.initParams(params, (resp) => {
})
let flag = mqtt.connect()
if (!flag) {
console.log("连接失败")
}
let topic = this.topic
// qos0 = 0 At least once delivery
// qos1
// /// Exactly once delivery
// case qos2
// /// !!! Used SUBACK frame only
// case FAILURE = 0x80
let qos = 0
mqtt.subscribe(topic, qos)
let topic = "xxx"
mqtt.unsubscribe(topic)
mqtt.publish(this.sendTopic, {
qos: 0,
retained: false,
payload: [0, 1] // payload支持字节数组或字符串
})
mqtt.disconnect()