当前位置: 首页 > news >正文

uniapp在app下使用mqtt协议!!!支持vue3

什么?打包空白?分享一下我的解决方法!

第一步
找大师算过了,装4.1版本运气好!
所以根目录执行命令…

npm install mqtt@4.1.0

第二步
自己封装一个mqtt文件方便后期开坛做法!

// utils/mqtt.js
import mqtt from 'mqtt/dist/mqtt'class MQTTClient {constructor() {this.client = nullthis.subscriptions = new Map()this.reconnectTimer = nullthis.config = {host: 'mqtt://your-broker.com',options: {clientId: 'uni-app-' + Date.now(),keepalive: 60,clean: true,reconnectPeriod: 5000}}}init() {if (!this.client) {this.connect()}}connect() {this.client = mqtt.connect(this.config.host, this.config.options)this.client.on('connect', () => {console.log('MQTT Connected')this.resubscribe()})this.client.on('message', (topic, message) => {this.handleMessage(topic, message)})this.client.on('error', (err) => {console.error('MQTT Error:', err)})this.client.on('close', () => {console.log('MQTT Connection closed')this.scheduleReconnect()})}subscribe(topic, callback) {if (!this.subscriptions.has(topic)) {this.subscriptions.set(topic, new Set())if (this.client?.connected) {this.client.subscribe(topic)}}this.subscriptions.get(topic).add(callback)}unsubscribe(topic, callback) {if (this.subscriptions.has(topic)) {const callbacks = this.subscriptions.get(topic)callbacks.delete(callback)if (callbacks.size === 0) {this.subscriptions.delete(topic)if (this.client?.connected) {this.client.unsubscribe(topic)}}}}handleMessage(topic, message) {if (this.subscriptions.has(topic)) {const callbacks = this.subscriptions.get(topic)callbacks.forEach(cb => cb(message.toString()))}}resubscribe() {if (this.client?.connected) {const topics = Array.from(this.subscriptions.keys())if (topics.length > 0) {this.client.subscribe(topics)}}}scheduleReconnect() {if (!this.reconnectTimer) {this.reconnectTimer = setTimeout(() => {this.reconnectTimer = nullthis.connect()}, 5000)}}destroy() {if (this.client) {this.client.end()this.client = null}this.subscriptions.clear()clearTimeout(this.reconnectTimer)}
}export const mqttClient = new MQTTClient()

第三步
打开 main.js 文件
思量前后,觉得还是全局挂载吧

import mqtt from '@/mqtt/dist/mqtt.js'
app.config.globalProperties.$mqtt = mqtt;

第四步
打开这个mqtt.js修改源码,注意,不是你自己创建的mqtt.js,是安装的依赖库文件,路径在根目的node_modules/mqtt/dist里面!!!!!
在这里插入图片描述
然后把里面的代码修改,看图,要改2行!!!源码使用的是 wx.connectSocket,修改之后:uni.connectSocket
最后要加上 complete:()=>{}, 别问为什么,一问你就输了!!!!
在这里插入图片描述
第五步
到这里已经可以使用了,不信你打包一下app试下,自定义基座也是没问题的!
下面是我的使用代码!

<template><view>收到的MQTT内容===>{{msg}}</view>
</template><script>export default {name: "wang-mqtt",data() {return {msg: '初始化mqtt'}},created() {// 连接配置let myOptions = {clientId: 'uni-app-' + Date.now(),keepalive: 60,clean: true,reconnectPeriod: 5000}let ip = ''// #ifdef H5ip = 'ws://你的IP:8083/mqtt'// #endif// #ifdef APP-PLUSip = 'wx://你的IP:8083/mqtt'// #endif// 创建 MQTT 客户端const client = this.$mqtt.connect(ip, myOptions);// 订阅主题client.subscribe('app_xxdg/topic', (err) => {if (!err) console.log('成功已订阅主题');});// 监听消息client.on('message', (topic, message) => {this.msg = message.toString()console.log(`收到消息:`, message.toString());});},methods: {}}
</script>
<style>
</style>
http://www.lryc.cn/news/542098.html

相关文章:

  • VMware虚拟机17.5.2版本下载与安装(详细图文教程包含安装包)
  • 如何加固织梦CMS安全,防webshell、防篡改、防劫持,提升DedeCMS漏洞防护能力
  • STM32的HAL库开发---ADC采集内部温度传感器
  • Linux 命令大全完整版(12)
  • Python - 代码片段分享 - Excel 数据实时写入方法
  • (七)趣学设计模式 之 适配器模式!
  • DeepSeek 细节之 MoE
  • 【Linux-网络】从逻辑寻址到物理传输:解构IP协议与ARP协议的跨层协作
  • 毕业离校管理系统的开发与需求分析
  • 【NLP 24、实践 ⑤ 计算Bert模型中的参数数量】
  • 一、Spring框架系统化学习路径
  • Midscene.js - AI驱动,轻松实现UI自动化
  • (九)Mapbox GL JS 中 Marker 图层的使用详解
  • 2k1000LA 使能 nand.
  • Junit+Mock
  • maven编译出错,javac: ��Ч��Ŀ�귢�а�: 17
  • Vue使用Three.js加载glb (gltf) 文件模型及实现简单的选中高亮、测距、测面积
  • <el-table>右侧有空白列解决办法
  • Linux网络 网络层
  • 系统讨论Qt的并发编程——逻辑上下文的分类
  • 《Linux Shell 脚本深度探索:原理与高效编程》
  • 深入剖析:基于红黑树实现自定义 map 和 set 容器
  • 在大数据项目中如何设计和优化数据模型
  • JavaScript querySelector()、querySelectorAll() CSS选择器解析(DOM元素选择)
  • Linux系统中处理子进程的终止问题
  • Docker 不再难懂:快速掌握容器命令与架构原理
  • 取消票证会把指定的票证从数据库中删除,同时也会把票证和航班 等相关表中的关联关系一起删除。但在删除之前,它会先检查当前用户是否拥有这张票
  • 力扣-贪心-763 划分字母区间
  • 【Redis 原理】网络模型
  • cpp中的继承