TypechoJoeTheme

Strjson博客-专注于各种精品源码、精品软件、技术教程分享、黑客技术、破解教程(爱你在心口难开、没事写一写)

统计

u_

子沫博主
2022-11-10
/
1 评论
/
2,174 阅读
/
138 个字
/
百度已收录
11/10
本文最后更新于2022年11月10日,已超过877天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
  1. 先安装Mqtt包,到你项目中的 node_modules目录下执行一下安装命令
  1. npm install mqtt --save
复制
  1. 创建一个配置文件js,建议放到utils目录下 mqtt.js
  1. export const MQTT_IP = ''//mqtt地址端口, 使用emqx时一定要加mqtt
  2. const MQTT_USERNAME = ''//mqtt用户名
  3. const MQTT_PASSWORD = ''//密码
  4. export const MQTT_OPTIONS = {
  5. connectTimeout: 5000,
  6. clientId: '',
  7. username: MQTT_USERNAME,
  8. password: MQTT_PASSWORD,
  9. clean: false,
  10. keepalive: 15,
  11. protocolVersion: 4, //MQTT连接协议版本
  12. resubscribe: true,
  13. reconnectPeriod: 1000,
  14. }
复制
  1. 在common中定义一个公用的mqtt.js各个需要使用的地方调用即可
  1. // "use strict";
  2. // Object.defineProperty(exports, "__esModule", {
  3. // value: !0
  4. // });
  5. import { v4 } from 'uuid';
  6. import {
  7. MQTT_IP,
  8. MQTT_OPTIONS
  9. } from '@/utils/mqtt.js';
  10. var mqtt = require('mqtt/dist/mqtt.js')
  11. var client;
  12. var topic=[
  13. 'online',
  14. 'movie',
  15. 'update',
  16. 'notice'
  17. ];
  18. var init=function(){
  19. let userInfos = uni.getStorageSync('userInfo');
  20. console.log('用户信息:'+userInfos)
  21. MQTT_OPTIONS.clientId =userInfos !=""?userInfos.mobile+'':v4();
  22. // #ifdef H5
  23. client = mqtt.connect('ws://' + MQTT_IP, MQTT_OPTIONS)
  24. // #endif
  25. // #ifdef MP-WEIXIN||APP-PLUS
  26. client = mqtt.connect('wx://' + MQTT_IP, MQTT_OPTIONS)
  27. // #endif
  28. client.on('connect', function() {
  29. console.log('连接成功')
  30. for (var i = 0; i < topic.length; i++){
  31. client.subscribe(topic[i], function(err) {
  32. if (!err) {
  33. console.log('订阅成功')
  34. }
  35. })
  36. }
  37. }).on('reconnect', function(error) {
  38. console.log('正在重连...')
  39. subscribeTopic();
  40. }).on('error', function(error) {
  41. console.log('连接失败...', error)
  42. }).on('end', function() {
  43. console.log('连接断开')
  44. }).on('message', function(topic, message) {
  45. console.log('接收推送信息:', message.toString());
  46. doSomeThing(topic,JSON.parse(message.toString()));
  47. }).on('close',function(re){
  48. console.log('链接关闭')
  49. })
  50. },
  51. sendMessage=function(t,str){
  52. client.publish(t, str, (e) => {
  53. console.log(e)
  54. })
  55. },
  56. doSomeThing=function(t,str){
  57. console.log('接收到的Topic:'+t)
  58. switch (t){
  59. case 'update':
  60. break;
  61. case 'movie':
  62. plus.push.createMessage(str.title, {}, {} );
  63. plus.push.addEventListener('click',msg=> {
  64. uni.navigateTo({
  65. url:"/pages/detail/index?id="+str.vid
  66. })
  67. });
  68. break;
  69. case 'online':
  70. break;
  71. case 'notice':
  72. break;
  73. default:
  74. break;
  75. }
  76. },
  77. subscribeTopic=function(){
  78. for (var i = 0; i < topic.length; i++){
  79. client.subscribe(topic[i], function(err) {
  80. if (!err) {
  81. console.log('订阅成功')
  82. }
  83. })
  84. }
  85. };
  86. export default {
  87. init: init,
  88. sendMessage: sendMessage,
  89. subscribeTopic: subscribeTopic,
  90. doSomeThing:doSomeThing
  91. };
复制
  1. 到main.js中定义mqttClient
  1. import mqttClient from '@/common/mqtt';
  2. Vue.prototype.$mqtt = mqttClient;
复制
  1. 具体mqtt的使用
  1. var s=e.device.deviceBrand; this.$mqtt.sendMessage('online','{"type":"upMB","brand":"'+s+'"}');// 进行线上的Topic发布
复制
朗读
赞(2)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

Strjson博客-专注于各种精品源码、精品软件、技术教程分享、黑客技术、破解教程(爱你在心口难开、没事写一写)

本文链接:

https://jpgke.com/jiaocheng/413.html(转载时请注明本文出处及文章链接)

评论 (1)
  1. 子沫 闲逛
    Windows 10 · Google Chrome

    2022-11-10 回复
#
R
G
B
H
S
B
OK