mqtt实现全局链接优化
parent
cb1e0a4d40
commit
2b69bf0d33
25
App.vue
25
App.vue
|
|
@ -15,12 +15,25 @@ export default {
|
||||||
onLaunch: function() {
|
onLaunch: function() {
|
||||||
this.initApp()
|
this.initApp()
|
||||||
mqttUtil.disconnectMqtt()
|
mqttUtil.disconnectMqtt()
|
||||||
|
|
||||||
|
// ========== 新增:H5刷新后,从localStorage恢复订阅列表 ==========
|
||||||
|
const savedSubscribeList = uni.getStorageSync('mqtt_subscribe_list')
|
||||||
|
if (savedSubscribeList && savedSubscribeList.length > 0) {
|
||||||
|
this.globalData.mqtt.subscribeList = savedSubscribeList
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onShow() {
|
onShow() {
|
||||||
console.log('小程序切前台/首次显示')
|
console.log('小程序切前台/首次显示')
|
||||||
const token = getToken() || this.globalData.mqtt.token
|
const token = getToken() || this.globalData.mqtt.token
|
||||||
if (token) {
|
if (token) {
|
||||||
console.info("token存在,mqtt重新连接中。。")
|
console.info("token存在,mqtt重新连接中。。")
|
||||||
|
// 兜底检查:如果globalData里没有列表,但缓存里有,补充恢复
|
||||||
|
if (this.globalData.mqtt.subscribeList.length === 0) {
|
||||||
|
const savedSubscribeList = uni.getStorageSync('mqtt_subscribe_list')
|
||||||
|
if (savedSubscribeList && savedSubscribeList.length > 0) {
|
||||||
|
this.globalData.mqtt.subscribeList = savedSubscribeList
|
||||||
|
}
|
||||||
|
}
|
||||||
this.reconnectMqtt()
|
this.reconnectMqtt()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -29,13 +42,11 @@ export default {
|
||||||
const mqttState = mqttUtil.getMqttState()
|
const mqttState = mqttUtil.getMqttState()
|
||||||
if (mqttState.isConnected) {
|
if (mqttState.isConnected) {
|
||||||
this.globalData.mqtt.subscribeList = mqttState.subscribeList
|
this.globalData.mqtt.subscribeList = mqttState.subscribeList
|
||||||
|
// ========== 新增:切后台时同步到localStorage ==========
|
||||||
|
uni.setStorageSync('mqtt_subscribe_list', this.globalData.mqtt.subscribeList)
|
||||||
mqttUtil.disconnectMqtt()
|
mqttUtil.disconnectMqtt()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onUnload() {
|
|
||||||
console.log('小程序销毁')
|
|
||||||
mqttUtil.disconnectMqtt()
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
// 初始化应用
|
// 初始化应用
|
||||||
initApp() {
|
initApp() {
|
||||||
|
|
@ -76,12 +87,16 @@ export default {
|
||||||
if (subscribeList.length > 0) {
|
if (subscribeList.length > 0) {
|
||||||
mqttUtil.updateSubscribeList(subscribeList)
|
mqttUtil.updateSubscribeList(subscribeList)
|
||||||
console.log('恢复MQTT订阅列表:', subscribeList)
|
console.log('恢复MQTT订阅列表:', subscribeList)
|
||||||
|
// ========== 新增:恢复订阅后同步到localStorage ==========
|
||||||
|
uni.setStorageSync('mqtt_subscribe_list', subscribeList)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
loginSuccess(token, subscribeList) {
|
loginSuccess(token, subscribeList) {
|
||||||
this.globalData.mqtt.hasLogin = true
|
this.globalData.mqtt.hasLogin = true
|
||||||
this.globalData.mqtt.token = token
|
this.globalData.mqtt.token = token
|
||||||
this.globalData.mqtt.subscribeList = subscribeList || []
|
this.globalData.mqtt.subscribeList = subscribeList || []
|
||||||
|
// ========== 新增:登录成功时同步到localStorage ==========
|
||||||
|
uni.setStorageSync('mqtt_subscribe_list', subscribeList || [])
|
||||||
this.reconnectMqtt()
|
this.reconnectMqtt()
|
||||||
console.log('登录成功,MQTT已初始化')
|
console.log('登录成功,MQTT已初始化')
|
||||||
},
|
},
|
||||||
|
|
@ -92,6 +107,8 @@ export default {
|
||||||
token: '',
|
token: '',
|
||||||
subscribeList: []
|
subscribeList: []
|
||||||
}
|
}
|
||||||
|
// ========== 新增:登出时清空localStorage的订阅列表 ==========
|
||||||
|
uni.removeStorageSync('mqtt_subscribe_list')
|
||||||
console.log('登出成功,MQTT已断开')
|
console.log('登出成功,MQTT已断开')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -389,6 +389,7 @@ export default {
|
||||||
ackMessage(topic, payload) {
|
ackMessage(topic, payload) {
|
||||||
// 1. 先判断是否是目标订阅主题(如dtu/xxx/up)
|
// 1. 先判断是否是目标订阅主题(如dtu/xxx/up)
|
||||||
if (topic !== this.mqttConfig.subscribeTopic) return;
|
if (topic !== this.mqttConfig.subscribeTopic) return;
|
||||||
|
// console.log(`监听到消息:topic=${topic},message=${payload}`)
|
||||||
|
|
||||||
// 2. 解析消息体(注意异常捕获)
|
// 2. 解析消息体(注意异常捕获)
|
||||||
let msgData = {};
|
let msgData = {};
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ export function connectMqtt() {
|
||||||
// 全局接收消息,转发给页面自定义回调
|
// 全局接收消息,转发给页面自定义回调
|
||||||
mqttState.client.on('message', (topic, payload) => {
|
mqttState.client.on('message', (topic, payload) => {
|
||||||
const message = payload.toString()
|
const message = payload.toString()
|
||||||
console.log(`收到MQTT消息:topic=${topic},message=${message}`)
|
// console.log(`收到MQTT消息:topic=${topic},message=${message}`)
|
||||||
if (typeof mqttState.onMessageCallback === 'function') {
|
if (typeof mqttState.onMessageCallback === 'function') {
|
||||||
mqttState.onMessageCallback(topic, message)
|
mqttState.onMessageCallback(topic, message)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue