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

云南网站建设小程序开发wordpress网站小屏

云南网站建设小程序开发,wordpress网站小屏,电子网站游戏网址试玩,中国新农村建设网站前文写了使用Docker部署了mqtt服务#xff0c;今天来写一下#xff0c;mqtt在web开发中的应用实例#xff0c;希望能帮到大家#xff0c;话不多说#xff0c;直接开始。 一#xff1a;脚手架安装mqtt 作者这里用的vue3的框架 直接上命令#xff0c;npm安装或者pnpm进行…前文写了使用Docker部署了mqtt服务今天来写一下mqtt在web开发中的应用实例希望能帮到大家话不多说直接开始。 一脚手架安装mqtt 作者这里用的vue3的框架 直接上命令npm安装或者pnpm进行安装。 npm install mqtt二使用mqtt 1.创建一个mqtt封装的js文件 比如我创建一个mqttTool.js 2.在js文件中引入mqtt import * as mqtt from mqtt/dist/mqtt.min.js;3.封装全局服务 const getMQTTUrl () {let url import.meta.env.VITE_APP_PHAR_MQTT;url ws://0.0.0.0:8000/mqtt; //填写自己的mqtt服务ipconsole.info(当前 MQTT 地址为 url);return url } //MQTT全局服务 export default class MQTTService {static instance null;static get Instance() {if (!this.instance) {this.instance new MQTTService();}return this.instance;}// 事件mitt new mitt();//配置参数userOptions {username: mqttName,password: test123,clientId: Math.random().toString(36).substr(2),};topics [];//[test1, dtest2]; //需要订阅的主题// 和服务端连接的MQTT对象client null;mqttUrl getMQTTUrl() ;// 标识是否连接成功connected false;// 记录重试的次数sendRetryCount 0;// 重新连接尝试的次数connectRetryCount 3;// 再次连接reconnect(){if (this.clientnull){this.connect()}}//定义连接服务器的方法connect() { let that this;this.client mqtt.connect(this.mqttUrl, this.userOptions);this.client.on(connect, (e) {this.connected true;// 重置重新连接的次数this.connectRetryCount 3;that.topics.forEach((topic) {that.client.subscribe(topic,{ qos: 0, retain: 0 },(err, granted) {if (granted.length 0) {if (!err) {console.log(全局成功订阅主题${granted[0].topic});} else {console.log(全局订阅主题失败, err);}}});});});//失败重连this.client.on(error, (e) {if (this.sendRetryCount this.connectRetryCount-1){console.log(连接服务端失败,e)this.connected false;this.sendRetryCountsetTimeout(() {this.connect();}, 500 * this.connectRetryCount);}});this.client.on(message, (topic, message, packet) {// 输出订阅的主题和消息console.log(接收到主题为 ${topic} 的消息, message.toString());let res {topic:topic,message:message,packet:packet}});}close(){ // 退出登录后断开连接if (this.client) {this.client.end(); //离开页面的时候 关闭mqtt连接this.client null;console.log(关闭mqtt);}}// 发送数据的方法send(data) {// 判断此时此刻有没有连接成功if (this.connected) {this.sendRetryCount 0;try {this.client.publish(data.sender, data);console.log(s发送到服务端,data);} catch (e) {this.client.publish(data.sender, JSON.stringify(data)); console.log(O发送到服务端,data)}} else {this.sendRetryCount;setTimeout(() {this.reconnect() // 重新连接this.client.publish(data.sender, JSON.stringify(data));}, this.sendRetryCount * 500);}}// 发送数据的方法2sendBySender(sender,data) {// 判断此时此刻有没有连接成功if (this.connected) {this.sendRetryCount 0;try {this.client.publish(sender, data);console.log(s发送到服务端,data)} catch (e) {this.client.publish(sender, JSON.stringify(data)); console.log(O发送到服务端,data)}} else {this.sendRetryCount;setTimeout(() {this.reconnect() // 重新连接this.client.publish(sender, JSON.stringify(data));}, this.sendRetryCount * 500);}}//自定义订阅内容setTopics(data){this.topics data;this.close();this.connect();}getTopics(){return this.topics;}//添加订阅addTopic(topic){this.topics.push(topic);if (this.connected) {this.client.subscribe(topic,{ qos: 0, retain: 0 },(err, granted) {if (granted.length 0) {if (!err) {console.log(全局成功订阅主题${granted[0].topic});} else {console.log(全局订阅主题失败, err);}}}); }else{this.connect();}} } 4.调用 在需要的vue中调用 import MQTTService from //utils/mqttTool.js let mqttServe MQTTService.Instance; 然后使用mqttServe对象调用相关方法即可。
http://www.zqtcl.cn/news/266013/

相关文章:

  • 做企业网站的轻量级cms建设电子商务网站流程图
  • 淘宝网站设计分析国内在线免费服务器
  • wordpress网站文章加密网站建设 博采网络
  • 哪个网站做美食好一点网络运维个人工作总结
  • 做网红用哪个网站教人做策划的网站
  • 百度免费网站怎样建设wordpress模板目录结构
  • 长沙简单的网站建设公司wordpress+手机应用
  • 用spl做网站wordpress不用缓存
  • 微网站模板标签网站被攻击怎么让百度重新蜘蛛自动抓
  • 自己想做一个网站网页背景怎么设置
  • 国外做项目的网站软件定制外包平台
  • 做网站要用什么软件房地产建设网站
  • 龙岗爱联有学网站建设装饰公司简介
  • pc端网站怎么做自适应哪个公司网站备案快
  • 品牌网站建设黑白I狼J烟台开发区建设业联合网站
  • 做视频网站可以自学吗php html5企业网站源码
  • 阿里云怎么部署网站引流推广平台是什么意思
  • 江山建设工程信息网站营销网讯
  • 网站制作公司 沈阳上海建设主管部门网站
  • 网站建设前期如何做好市场定位分析网络推广主要工作内容
  • 做一个网站的流程是什么金融网站建设方案
  • 汽车维修保养网站模板北京网站建设知名公司排名
  • 网站建设案例分享网络推广网
  • 广州知名网站推广app软件开发制作公司电话
  • 泉州专业网站建设seo是指什么职位
  • 怎么做房产网站张家港高端网站制作
  • 做网站运营公司收费广东短视频seo搜索哪家好
  • 外贸网站 源码做的好详情页网站
  • 冀州网站制作邢台百姓网官网
  • 佛山做外贸网站方案自助网站推广系统