uniapp制作APP的mqtt链接问题

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') || ''
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值