1.mqtt连接的URL
(1)ws://cloud.u.com:8083/mqtt
APP打包之后无法使用,其他都能使用。
(2)wss://cloud.u.com:8084/mqtt
加强版
(3)emqx.u.com代替cloud.u.com
mqtt连接一般使用这个路径
(4)APP专用路径,只有这个路径打包后才能正常
wx://emqx.u.com:8083/mqtt
2.APP的mqtt连接巨坑
(1)用ws://cloud.u.com:8083/mqtt,在mqtt未打包完成之前都是正常的
(2)mqtt连接的代码不能卸载store的mqtt.js文件中,一般写在login.vue中。APP专用路径在store中无法使用
3.mqtt 连接的流程
(1)建立连接(clientId要用随机数,不能写死)
(2)订阅主题
(3)监听数据,在任何一个文件中都能监听数据
(4)登出时清除mqtt连接
4.mqtt连接与store的问题
(1)比如=》store里面存了几个数据:wstopic、wstoken、mqttClient
(2)引用时 import { mapActions, mapGetters } from “vuex”;
computed: {
...mapGetters([
'wstoken',
'wstopic',
'mqttClient',
])
}
(3)直接调用时this.mqttClient 、this.wstopic
(4)带事件时 this. s t o r e . m q t t C l i e n t . o n ( ′ m e s s a g e ′ , ( t o p i c , m e s s a g e ) = > ) ( 5 )存储数据时 t h i s . store.mqttClient.on('message', (topic, message) => {}) (5)存储数据时 this. store.mqttClient.on(′message′,(topic,message)=>)(5)存储数据时this.store.commit(‘SET_WSTOPIC’, wstopic)
(6)清空时 this.$store.mqttClient.end(true);
5.APP-MQTT连接代码
(1)store 中的 mqtt.js文件
const mqtt = {
state: {
wstoken: uni.getStorageSync('wstoken') || ''