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

广东南电建设集团网站河南公司网站建设

广东南电建设集团网站,河南公司网站建设,创意模板,网站栏目内链怎么做什么?打包空白?分享一下我的解决方法! 第一步 找大师算过了,装4.1版本运气好! 所以根目录执行命令… npm install mqtt4.1.0第二步 自己封装一个mqtt文件方便后期开坛做法! // utils/mqtt.js import mqt…

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

第一步
找大师算过了,装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.dtcms.com/wzjs/237034.html

相关文章:

  • 政府网站谁做的seo技术服务外包
  • 在兔展上怎么做网站页面枸橼酸西地那非片功效效及作用
  • 网站域名更换是怎么做的上首页的seo关键词优化
  • 百度竞价排名规则及费用重庆seo结算
  • 淄博市网站建设西安网站设计
  • 顺德网站建设收费标准百度网盘电脑版
  • 建设单位到江川区住房和城乡建设局网站seo外包优化服务商
  • 厦门国外网站建设公司排名网站推广营销运营方式
  • 学技巧网站制作做百度推广效果怎么样
  • 东莞塘厦网站制作今日军事头条
  • 程家桥街道网站建设优化大师优化项目有
  • 物流公司网站怎么做长春网站建设路
  • 怎么用html做图片展示网站做网站seo怎么赚钱
  • 百度seo网站在线诊断商城推广软文范文
  • 礼品网站建设网页设计的流程
  • 安徽省住房和建设厅门户网站怎么做营销推广
  • 海珠区pc端网站建设中国职业技能培训中心官网
  • 婚庆网站哪个靠谱好的竞价账户托管外包
  • 大型网站 空间网站广告调词平台
  • 旅游网站的建设内容bt磁力搜索引擎在线
  • 网站建设分金手指专业二西安seo阳建
  • 石家庄市网站制作价格武汉网站运营专业乐云seo
  • 专门做特卖的网站是什么意思八种营销模式
  • 响应式布局方案seo网站推广seo
  • 做电力项目信息的网站torrentkitty磁力搜索引擎
  • 做动态图网站有哪些产品线上推广渠道
  • 紫金网站制作策划百度的主页
  • 织梦做分类信息系统网站杭州做百度推广的公司
  • 网络公司做什么业务上海seo优化服务公司
  • 建网站要多长时间适合小学生的新闻事件