前端mqtt的具体利用(包罗mqtt服务器部署,前端vue3利用mqtt连接、订阅主题 ...

海哥  金牌会员 | 2024-7-25 23:38:53 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 889|帖子 889|积分 2667

一、简述

​ MQTT(消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的通讯协议,该协议构建于TCP/IP协议上。MQTT最大优点在于,可以以极少的代码和有限的带宽,为连接长途装备提供及时可靠的消息服务。MQTT 协议的应用场景包括物联网、移动应用、车联网、智能家居、即时聊天等。
二、特性



  • 利用发布/订阅消息模式。
  • 对负载内容屏蔽的消息传输。
  • 利用TCP/IP提供网络连接。
  • 有三种消息发布服务质量:

    • “至多一次”,消息发布完全依赖底层TCP/IP网络。会发生消息丢失或重复。这一级别可用于如下情况,环境传感器数据,丢失一次读记载无所谓,由于不久后还会有第二次发送。这一种方式主要平凡APP的推送,倘若你的智能装备在消息推送时未联网,推送过去充公到,再次联网也就收不到了。
    • “至少一次”,确保消息到达,但消息重复可能会发生。
    • “只有一次”,确保消息到达一次。在一些要求比较严酷的计费体系中,可以利用此级别。在计费体系中,消息重复或丢失会导致不正确的效果。这种最高质量的消息发布服务还可以用于即时通讯类的APP的推送,确保用户收到且只会收到一次。

  • 小型传输,开销很小(固定长度的头部是2字节),协议交换最小化,以低落网络流量。
  • 利用Last Will和Testament特性通知有关各方客户端异常停止的机制。
    Last Will:即遗言机制,用于通知同一主题下的其他装备发送遗言的装备已经断开了连接。
    Testament:遗嘱机制,功能类似于Last Will。
三、MQTT协议中的订阅、主题、会话

1.订阅(Subscription)
订阅包罗主题筛选器(Topic Filter)和最大服务质量(QoS)。订阅会与一个会话(Session)关联。一个会话可以包罗多个订阅。每一个会话中的每个订阅都有一个差别的主题筛选器。
2.会话(Session)
每个客户端与服务器建立连接后就是一个会话,客户端和服务器之间有状态交互。会话存在于一个网络之间,也可能在客户端和服务器之间超过多个一连的网络连接。
3.主题名(Topic Name)
连接到一个应用程序消息的标签,该标签与服务器的订阅相匹配。服务器会将消息发送给订阅所匹配标签的每个客户端。
4.主题筛选器(Topic Filter)
一个对主题名通配符筛选器,在订阅表达式中利用,体现订阅所匹配到的多个主题。
5.负载(Payload)
消息订阅者所具体吸收的内容。
四、MQTT协议中的方法



  • Connect。等待与服务器建立连接。
  • Disconnect。等待MQTT客户端完成所做的工作,并与服务器断开TCP/IP会话。
  • Subscribe。等待完成订阅。
  • UnSubscribe。等待服务器取消客户端的一个或多个topics订阅。
  • Publish。MQTT客户端发送消息请求,发送完成后返回应用程序线程。
五、前端利用mqtt

1. mqtt服务器的部署

利用 EMQX CLOUD 进行mqtt服务的部署
EMQX Cloud: 全托管的 MQTT 消息云服务
部署教程:https://www.bilibili.com/video/BV1Bx4y1K7hN/
利用Serverless版本每月有免费额度

2. 安装js的mqtt包

  1. npm i mqtt
复制代码
3. 前端代码

完整代码已上传至github
https://github.com/void00013/8.mqtt_vue3_test/tree/main
请自行下载依赖后运行
  1. // Home.vue
  2. <template>
  3.   <div class="home-container">
  4.     <el-card shadow="always" style="margin-bottom:30px;">
  5.       <div class="emq-title">
  6.         Configuration
  7.       </div>
  8.       <el-form ref="configForm" hide-required-asterisk size="small" label-position="top" :model="connection">
  9.         <el-row :gutter="20">
  10.           <el-col :span="8">
  11.             <el-form-item prop="host" label="Host">
  12.               <el-row :gutter="10">
  13.                 <el-col :span="7">
  14.                   <el-select v-model="connection.protocol" @change="handleProtocolChange">
  15.                     <el-option label="ws://" value="ws"></el-option>
  16.                     <el-option label="wss://" value="wss"></el-option>
  17.                   </el-select>
  18.                 </el-col>
  19.                 <el-col :span="17">
  20.                   <el-input v-model="connection.host"></el-input>
  21.                 </el-col>
  22.               </el-row>
  23.             </el-form-item>
  24.           </el-col>
  25.           <el-col :span="8">
  26.             <el-form-item prop="port" label="Port">
  27.               <el-input v-model.number="connection.port" type="number" placeholder="8083/8084"></el-input>
  28.             </el-form-item>
  29.           </el-col>
  30.           <el-col :span="8">
  31.             <el-form-item prop="endpoint" label="Mountpoint">
  32.               <el-input v-model="connection.endpoint" placeholder="/mqtt"></el-input>
  33.             </el-form-item>
  34.           </el-col>
  35.           <el-col :span="8">
  36.             <el-form-item prop="clientId" label="Client ID">
  37.               <el-input v-model="connection.clientId"> </el-input>
  38.             </el-form-item>
  39.           </el-col>
  40.           <el-col :span="8">
  41.             <el-form-item prop="username" label="Username">
  42.               <el-input v-model="connection.username"></el-input>
  43.             </el-form-item>
  44.           </el-col>
  45.           <el-col :span="8">
  46.             <el-form-item prop="password" label="Password">
  47.               <el-input v-model="connection.password"></el-input>
  48.             </el-form-item>
  49.           </el-col>
  50.           <el-col :span="24">
  51.             <el-button type="success" size="small" class="conn-btn" style="margin-right: 20px;" :disabled="client.connected" @click="createConnection" :loading="connecting">
  52.               {{ client.connected ? 'Connected' : 'Connect' }}
  53.             </el-button>
  54.             <el-button v-if="client.connected" type="danger" size="small" class="conn-btn" @click="destroyConnection">
  55.               Disconnect
  56.             </el-button>
  57.           </el-col>
  58.         </el-row>
  59.       </el-form>
  60.     </el-card>
  61.     <el-card shadow="always" style="margin-bottom:30px;">
  62.       <div class="emq-title">
  63.         Subscribe
  64.       </div>
  65.       <el-form ref="subscription" hide-required-asterisk size="small" label-position="top" :model="subscription">
  66.         <el-row :gutter="20">
  67.           <el-col :span="8">
  68.             <el-form-item prop="topic" label="Topic">
  69.               <el-input v-model="subscription.topic"></el-input>
  70.             </el-form-item>
  71.           </el-col>
  72.           <el-col :span="8">
  73.             <el-form-item prop="qos" label="QoS">
  74.               <el-select v-model="subscription.qos">
  75.                 <el-option v-for="qos in qosList" :key="qos" :label="qos" :value="qos"></el-option>
  76.               </el-select>
  77.             </el-form-item>
  78.           </el-col>
  79.           <el-col :span="8">
  80.             <el-button :disabled="!client.connected" type="success" size="small" class="subscribe-btn" @click="doSubscribe">
  81.               {{ subscribeSuccess ? 'Subscribed' : 'Subscribe' }}
  82.             </el-button>
  83.             <el-button :disabled="!client.connected" type="success" size="small" class="subscribe-btn" style="margin-left:20px" @click="doUnSubscribe" v-if="subscribeSuccess">
  84.               Unsubscribe
  85.             </el-button>
  86.           </el-col>
  87.         </el-row>
  88.       </el-form>
  89.     </el-card>
  90.     <el-card shadow="always" style="margin-bottom:30px;">
  91.       <div class="emq-title">
  92.         Publish
  93.       </div>
  94.       <el-form ref="publish" hide-required-asterisk size="small" label-position="top" :model="publish">
  95.         <el-row :gutter="20">
  96.           <el-col :span="8">
  97.             <el-form-item prop="topic" label="Topic">
  98.               <el-input v-model="publish.topic"></el-input>
  99.             </el-form-item>
  100.           </el-col>
  101.           <el-col :span="8">
  102.             <el-form-item prop="payload" label="Payload">
  103.               <el-input v-model="publish.payload"></el-input>
  104.             </el-form-item>
  105.           </el-col>
  106.           <el-col :span="8">
  107.             <el-form-item prop="qos" label="QoS">
  108.               <el-select v-model="publish.qos">
  109.                 <el-option v-for="qos in qosList" :key="qos" :label="qos" :value="qos"></el-option>
  110.               </el-select>
  111.             </el-form-item>
  112.           </el-col>
  113.         </el-row>
  114.       </el-form>
  115.       <el-col :span="24">
  116.         <el-button :disabled="!client.connected" type="success" size="small" class="publish-btn" @click="doPublish">
  117.           Publish
  118.         </el-button>
  119.       </el-col>
  120.     </el-card>
  121.     <el-card shadow="always" style="margin-bottom:30px;">
  122.       <div class="emq-title">
  123.         Receive
  124.       </div>
  125.       <el-col :span="24">
  126.         <el-input type="textarea" :rows="3" style="margin-bottom: 15px" v-model="receiveNews" readOnly></el-input>
  127.       </el-col>
  128.     </el-card>
  129.   </div>
  130. </template>
  131. <script>
  132. import mqtt from 'mqtt'
  133. export default {
  134.   name: 'Home',
  135.   data() {
  136.     return {
  137.       connection: {
  138.         protocol: 'wss',
  139.         host: '你的mqtt服务器地址',
  140.         // server less服务器只有两种协议:mqtts: 8883; wss: 8084
  141.         port: 8084,
  142.         endpoint: '/mqtt',
  143.         clean: true,
  144.         connectTimeout: 30 * 1000, // ms
  145.         reconnectPeriod: 4000, // ms
  146.         clientId: 'emqx_vue_' + Math.random().toString(16).substring(2, 8),
  147.         // auth
  148.         username: 'void',
  149.         password: '123',
  150.       },
  151.       subscription: {
  152.         topic: 'topic/mqttx',
  153.         qos: 0,
  154.       },
  155.       publish: {
  156.         topic: 'topic/browser',
  157.         qos: 0,
  158.         payload: '{ "msg": "Hello, I am browser." }',
  159.       },
  160.       receiveNews: '',
  161.       qosList: [0, 1, 2],
  162.       client: {
  163.         connected: false,
  164.       },
  165.       subscribeSuccess: false,
  166.       connecting: false,
  167.       retryTimes: 0,
  168.     }
  169.   },
  170.   methods: {
  171.     initData() {
  172.       this.client = {
  173.         connected: false,
  174.       }
  175.       this.retryTimes = 0
  176.       this.connecting = false
  177.       this.subscribeSuccess = false
  178.     },
  179.     handleOnReConnect() {
  180.       this.retryTimes += 1
  181.       if (this.retryTimes > 5) {
  182.         try {
  183.           this.client.end()
  184.           this.initData()
  185.           this.$message.error('Connection maxReconnectTimes limit, stop retry')
  186.         } catch (error) {
  187.           this.$message.error(error.toString())
  188.         }
  189.       }
  190.     },
  191.     createConnection() {
  192.       try {
  193.         this.connecting = true
  194.         const { protocol, host, port, endpoint, ...options } = this.connection
  195.         const connectUrl = `${protocol}://${host}:${port}${endpoint}`
  196.         this.client = mqtt.connect(connectUrl, options)
  197.         if (this.client.on) {
  198.           this.client.on('connect', () => {
  199.             this.connecting = false
  200.             console.log('Connection succeeded!')
  201.           })
  202.           this.client.on('reconnect', this.handleOnReConnect)
  203.           this.client.on('error', (error) => {
  204.             console.log('Connection failed', error)
  205.           })
  206.           this.client.on('message', (topic, message) => {
  207.             this.receiveNews = this.receiveNews.concat(message)
  208.             console.log(`Received message ${message} from topic ${topic}`)
  209.           })
  210.         }
  211.       } catch (error) {
  212.         this.connecting = false
  213.         console.log('mqtt.connect error', error)
  214.       }
  215.     },
  216.     // subscribe topic
  217.     doSubscribe() {
  218.       const { topic, qos } = this.subscription
  219.       this.client.subscribe(topic, { qos }, (error, res) => {
  220.         if (error) {
  221.           console.log('Subscribe to topics error', error)
  222.           return
  223.         }
  224.         this.subscribeSuccess = true
  225.         console.log('Subscribe to topics res', res)
  226.       })
  227.     },
  228.     // unsubscribe topic
  229.     doUnSubscribe() {
  230.       const { topic } = this.subscription
  231.       this.client.unsubscribe(topic, (error) => {
  232.         if (error) {
  233.           console.log('Unsubscribe error', error)
  234.         }
  235.       })
  236.     },
  237.     // publish message
  238.     doPublish() {
  239.       const { topic, qos, payload } = this.publish
  240.       this.client.publish(topic, payload, { qos }, (error) => {
  241.         if (error) {
  242.           console.log('Publish error', error)
  243.         }
  244.       })
  245.     },
  246.     // disconnect
  247.     destroyConnection() {
  248.       if (this.client.connected) {
  249.         try {
  250.           this.client.end(false, () => {
  251.             this.initData()
  252.             console.log('Successfully disconnected!')
  253.           })
  254.         } catch (error) {
  255.           console.log('Disconnect failed', error.toString())
  256.         }
  257.       }
  258.     },
  259.     handleProtocolChange(value) {
  260.       this.connection.port = value === 'wss' ? '8084' : '8083'
  261.     },
  262.   },
  263. }
  264. </script>
  265. <style lang="scss">
  266. @import url('../assets/style/home.scss');
  267. .home-container {
  268.   max-width: 1100px;
  269.   margin: 0 auto;
  270.   .conn-btn {
  271.     color: #fff;
  272.     background-color: #00b173;
  273.     font-size: 14px;
  274.   }
  275.   .publish-btn {
  276.     margin-bottom: 20px;
  277.     float: right;
  278.   }
  279.   .el-button--success {
  280.     background-color: #34c388 !important;
  281.     border-color: #34c388 !important;
  282.     font-size: 14px !important;
  283.   }
  284.   .el-button--danger {
  285.     background-color: #f5222d !important;
  286.     border-color: #f5222d !important;
  287.   }
  288.   .el-form-item {
  289.     &.is-error {
  290.       .el-input__inner,
  291.       .el-textarea__inner {
  292.         box-shadow: 0 0 0 2px rgba(245, 34, 45, 0.2);
  293.       }
  294.     }
  295.     &.is-success {
  296.       .el-input__inner,
  297.       .el-textarea__inner {
  298.         border-color: #34c388 !important;
  299.       }
  300.     }
  301.   }
  302. }
  303. </style>
复制代码
  1. // home.scss
  2. body {
  3.   background-color: #f0f2f5;
  4.   padding: 0;
  5.   margin: 0;
  6.   color: rgba(0, 0, 0, 0.65);
  7.   font-size: 14px;
  8.   font-family: -apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei,
  9.     Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;
  10. }
  11. .emq-title {
  12.   font-size: 16px;
  13.   color: #333333;
  14.   font-weight: bolder;
  15.   margin-bottom: 15px;
  16.   &.h3 {
  17.     font-size: 14px;
  18.   }
  19.   &[size='small'] {
  20.     font-size: 14px;
  21.   }
  22.   .sub-title {
  23.     font-weight: normal;
  24.     display: block;
  25.     font-size: 12px;
  26.     color: #8f9297;
  27.     margin-top: 12px;
  28.   }
  29.   &.required-title {
  30.     &:before {
  31.       content: '*';
  32.       color: #f5222d;
  33.       margin-right: 4px;
  34.     }
  35.   }
  36. }
  37. .el-select {
  38.   width: 100%;
  39. }
  40. .subscribe-btn {
  41.   margin-top: 42px !important;
  42. }
  43. .publish-btn {
  44.   margin-bottom: 30px;
  45. }
复制代码
4. EMQX官方示例

官方提供了vue、react、java、python等各种语言的代码案例,如有必要请自行查察
https://github.com/emqx/MQTT-Client-Examples/tree/master

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

海哥

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表