当前位置:   article > 正文

uniapp使用Mqtt-页面实时更新数据_uniapp mqtt

uniapp mqtt

效果图

以下代码使用mqtt兼容H5、App、微信小程序、支付宝小程序

一.安装mqtt依赖包

1.H5和App、微信小程序,统一使用依赖包的mqtt文件

npm  i  mqtt@3.0.0

2.支付宝小程序,需要在官网下载mqtt.min.js文件,存放路径:/utils/mqtt.min.js

下载js文件链接: https://pan.baidu.com/s/1KHE7FRa_GGQE0DM1U4pjcQ?pwd=5xuy

参考文档:快速使用MQTT 支付宝小程序 SDK实现消息收发 [IM 开发文档]

参考github代码:GitHub - Wster11/zhifubao-mqtt-demo

二.封装成工具包

/utils/sendMqtt.js 

  1. // #ifdef H5
  2. const mqtturl = "wss://";
  3. var mqtt = require('mqtt/dist/mqtt.js')
  4. // #endif
  5. // #ifdef APP-PLUS || MP-WEIXIN
  6. const mqtturl = "wxs://";
  7. var mqtt = require('mqtt/dist/mqtt.js')
  8. //#endif
  9. // #ifdef MP-ALIPAY
  10. const mqtturl = "alis://";
  11. var mqtt = require('@/utils/mqtt.min.js')
  12. // import mqtt from "@/utils/mqtt.min.js" // 或者这样引入
  13. //#endif
  14. var client;
  15. // 创建MQTT连接
  16. const createMqtt = () => {
  17. let options = {
  18. keepalive: 1, // 心跳时间最小值可以为0,表示客户端不断开。 假如设置为0 断开网络监听不到 手机应用缩小化mqtt掉线后 根据心跳时间重新请求连接mqtt
  19. clientId: "你的clientId",
  20. protocolId: 'MQTT',
  21. username: "你的username",
  22. password: "你的password",
  23. protocolVersion: 4,
  24. clean: true,
  25. reconnectPeriod: 1000, // reconnectPeriod为1000毫秒,这意味着在连接丢失之后,客户端将在1秒后尝试重新连接。
  26. connectTimeout: 5000, // 5s超时时间 意味着mqtt-reconnect函数5秒钟触发一次
  27. topic: "你的topic",
  28. rejectUnauthorized: false,
  29. // #ifdef MP-ALIPAY
  30. my: my,//注意这里的my
  31. //#endif
  32. }
  33. try {
  34. if (!client) {
  35. client = mqtt.connect("你的mqtturl", options);
  36. initEventHandleMqtt(options.topic);
  37. }
  38. } catch (e) {}
  39. };
  40. //建立连接
  41. const initEventHandleMqtt = (topicUrl) => {
  42. client.on("connect", function() {
  43. uni.hideLoading()
  44. console.log("mqtt连接成功");
  45. //订阅主题 presence
  46. client.subscribe(topicUrl, function(err) {
  47. console.log("mqtt订阅主题");
  48. });
  49. });
  50. //如果mqttws订阅主题成功,那么这里就是当接收到自己订阅主题的处理逻辑
  51. client.on("message", function(topic, message) {
  52. // 获取信息
  53. const mqttData = JSON.parse(message);
  54. // 传递信息
  55. uni.$emit("mqttData", mqttData);
  56. });
  57. // 重新连接
  58. client.on('reconnect', function() {
  59. uni.showLoading({
  60. title: "重新连接"
  61. })
  62. })
  63. //如果连接错误,打印错误
  64. client.on("error", function(err) {
  65. console.log('mqtt---error...', err)
  66. });
  67. // 断开连接
  68. client.on('disconnect', function() {})
  69. // 连接断开后触发的回调
  70. client.on("close", function() {});
  71. // 掉线
  72. client.on("offline", function() {});
  73. // 断开
  74. client.on('end', () => {});
  75. };
  76. //强制断开Mqtt
  77. const closeMqtt = () => {
  78. client?.end()
  79. client = null
  80. };
  81. // 使用心跳 判断客户端和服务端是否还在连接着
  82. const judgeBeat = () => {
  83. if (client && client.pingResp === false) {}
  84. }
  85. export {
  86. createMqtt,
  87. closeMqtt,
  88. client,
  89. }

三.在页面调用

接收到的数据,给页面赋值,实现实时更新页面数据

  1. <script>
  2. import {
  3. createMqtt
  4. } from '@/utils/sendMqtt.js';
  5. export default {
  6. onLoad() {
  7. createMqtt();
  8. },
  9. mounted() {
  10. // 接收Mqtt信息
  11. uni.$on('mqttData', (msgData) => {
  12. });
  13. },
  14. }
  15. </script>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/824677
推荐阅读
相关标签
  

闽ICP备14008679号