Vue项目中MQTT如何使用
发表于:2024-09-27 作者:千家信息网编辑
千家信息网最后更新 2024年09月27日,本篇文章为大家展示了 Vue项目中MQTT如何使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。安装 MQTT 客户端库通过命令行安装:可以使用 npm 或
千家信息网最后更新 2024年09月27日Vue项目中MQTT如何使用
本篇文章为大家展示了 Vue项目中MQTT如何使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
安装 MQTT 客户端库
通过命令行安装:
可以使用 npm 或 yarn 命令,二者选一
npm install mqtt --save
yarn add mqtt
通过 CDN 引入
下载到本地,然后使用相对路径引入
MQTT 的使用
连接 MQTT 服务器
本文将使用 EMQ X 提供的 免费公共 MQTT 服务器,该服务基于 EMQ X 的 MQTT 物联网云平台 创建。服务器接入信息如下:
Broker: broker.emqx.io
TCP Port: 1883
Websocket Port: 8083
连接关键代码:
订阅主题
doSubscribe() { const { topic, qos } = this.subscription this.client.subscribe(topic, qos, (error, res) => { if (error) { console.log('Subscribe to topics error', error) return } this.subscribeSuccess = true console.log('Subscribe to topics res', res) })},
取消订阅
doUnSubscribe() { const { topic } = this.subscription this.client.unsubscribe(topic, error => { if (error) { console.log('Unsubscribe error', error) } })}
消息发布
doPublish() { const { topic, qos, payload } = this.publication this.client.publish(topic, payload, qos, error => { if (error) { console.log('Publish error', error) } })}
断开连接
destroyConnection() { if (this.client.connected) { try { this.client.end() this.client = { connected: false, } console.log('Successfully disconnected!') } catch (error) { console.log('Disconnect failed', error.toString()) } }}
上述内容就是 Vue项目中MQTT如何使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
加密
服务
服务器
项目
信息
内容
命令
技能
时间
知识
程序
订阅
简明
简明扼要
主题
代码
关键
字符
字符串
客户
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
黑龙江云端服务器托管服务器
优购物软件开发
武汉宇天互联网科技有限公司
数据库同步文字与格式错误
人类如何破坏水的数据库
谢江网络安全
广州erp软件开发报价
数据库和多媒体技术
计算机网络技术线路连接
分销软件开发公司有哪些
c 怎么更改数据库连接
余姚安卓软件开发系统
贝壳找房软件开发者
贝壳信息中心软件开发部
米家摄像头连不上服务器
服务器自动限速断网
有数据库ocm证书能进大公司吗
山东安卓软件开发定制费用
网络安全软件还是硬件
数据库系统安全的重要性
无线网络安全管控
四川便民平台软件开发公司
北京房保互联网科技有限公司
数据库 表锁 行锁
电脑网络技术可以干什么的
两个数据库数据比对
计算机网络技术认证书
通过堡垒主机登录不上服务器
微架构 软件开发模式
陕西回收服务器主板云服务器