关于mqtt+js前端中mqtt服务器关闭重连服务器后js前端接收不到消息的问题
解决方案
const MQTT_OPTIONS = {
connectTimeout: 10000,
clientId: '',
username: 'admin',
password: 'public',
clean: true,
}
connectMqtt1() {
MQTT_OPTIONS.clientId = v4.v4()
const client = mqtt.connect("ws://172.16.40.118:8083/mqtt", MQTT_OPTIONS)
client.on("connect", () => {
client.subscribe("M3/#", (err) => {
if (!err) {
console.log('订阅成功!')
}
})
}).on('reconnect', (err) => {
console.log('正在重连...' + this.topic)
client.subscribe("M3/#")
}).on('end', (err) => {
console.log('连接断开!')
}).on("message", (topic, message) => {
try {
let data = JSON.parse(message)
} catch (e) {
console.log("Caught: " + e.message)
}
})
}
|