欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Vue中使用MQTT實現(xiàn)通信過程

 更新時間:2025年07月30日 09:31:15   作者:s_Myint  
文章介紹了在Vue項目中集成MQTT的步驟:安裝mqtt.js庫,創(chuàng)建MQTT連接工具類以實現(xiàn)復(fù)用,通過Vue組件或直接在頁面使用MQTT客戶端,最后強調(diào)這是個人經(jīng)驗分享,鼓勵支持腳本之家

一、安裝 MQTT 客戶端庫

我們使用 mqtt.js,它是一個支持瀏覽器環(huán)境的 MQTT 客戶端庫。

通過以下命令安裝:

npm install mqtt

二、創(chuàng)建 MQTT 連接工具類

為了方便管理和復(fù)用 MQTT 客戶端,我們創(chuàng)建一個工具類 mqttClient.js,放在 src/utils 目錄下:

import mqtt from 'mqtt';

class MQTTClient {
  constructor(options) {
    this.client = null;
    this.messages = [];
    this.subscribers = {};
    this.defaultOptions = {
      clientId: 'vue-client_' + Math.random().toString(16).substr(2, 8),
      clean: true,
      connectTimeout: 4000,
      //username: '賬號',
      //password: '密碼'
    };
    this.options = { ...this.defaultOptions, ...options };
  }

  connect(brokerUrl) {
    return new Promise((resolve, reject) => {
      try {
        this.client = mqtt.connect(brokerUrl, this.options);

        this.client.on('connect', () => {
          console.log('連接成功');
          resolve(this.client);
        });

        this.client.on('error', (err) => {
          console.error('連接失敗:', err);
          reject(err);
        });

        this.client.on('message', (topic, payload) => {
          const message = {
            topic: topic,
            content: payload.toString()
          };
          this.messages.push(message);
          
          // 如果有訂閱者,通知所有訂閱了該主題的回調(diào)
          if (this.subscribers[topic]) {
            this.subscribers[topic].forEach(callback => callback(message));
          }
        });

        this.client.on('close', () => {
          console.log('MQTT connection closed');
        });
      } catch (err) {
        reject(err);
      }
    });
  }
// 訂閱主題
  subscribe(topic, callback, qos = 0) {
    if (!this.client || this.client.connected === false) {
      // console.error('MQTT not connected');
      return;
    }

    this.client.subscribe(topic, { qos }, (err) => {
      if (!err) {
        console.log(`Subscribed to ${topic}`);
        // 存儲訂閱回調(diào)
        if (!this.subscribers[topic]) {
          this.subscribers[topic] = [];
        }
        this.subscribers[topic].push(callback);
      }
    });
  }
// 取消訂閱指定主題的方法
  unsubscribe(topic) {
    if (!this.client || this.client.connected === false) {
      console.error('MQTT not connected');
      return;
    }

    this.client.unsubscribe(topic, (err) => {
      if (!err) {
        console.log(`Unsubscribed from ${topic}`);
        // 移除訂閱回調(diào)
        if (this.subscribers[topic]) {
          delete this.subscribers[topic];
        }
      }
    });
  }
// 發(fā)送消息
  publish(topic, message, qos = 0, retain = false) {
    if (!this.client || this.client.connected === false) {
      console.error('MQTT not connected');
      return;
    }

    this.client.publish(topic, message, { qos, retain }, (err) => {
      if (err) {
        console.error('Publish error:', err);
      }
    });
  }
// 取消連接
  disconnect() {
    if (this.client) {
      this.client.end();
      this.client = null;
    }
  }

  getMessages() {
    return [...this.messages];
  }

  clearMessages() {
    this.messages = [];
  }
}

export default MQTTClient;

三、在 Vue 組件中使用

我們創(chuàng)建一個 Vue 組件來使用 MQTT 客戶端

<template>
  <div>
    <h3>接收的 MQTT 消息:</h3>
    <ul>
      <li v-for="(msg, index) in messages" :key="index">
        {{ msg.topic }}: {{ msg.content }}
      </li>
    </ul>
    <!-- <ul>
      <li v-for="(msg, index) in top2" :key="index">
        {{ msg.topic }}: {{ msg.content }}
      </li>
    </ul> -->
    <button @click="publishMessage">發(fā)送消息</button>
  </div>
</template>

<script>
import MQTTClient from '@/util/mqtt';

export default {
  data() {
    return {
      mqttClient: null,
      messages: [],
      top2:[]
    };
  },
  mounted() {
    this.initMQTT();
  },
  methods: {
    initMQTT() {
      this.mqttClient = new MQTTClient();
      
      // 連接到 MQTT 代理
       const url="ws://"+"你的地址"+":8083/mqtt"  //例如ws://172.18.14.167:8083/mqtt
      this.mqttClient.connect(url)
        .then(() => {
          console.log('訂閱成功');
          // 訂閱主題
          this.mqttClient.subscribe('kpmqqt-lims-data-top1', (message) => {
            this.messages.push(message);
          });
          // this.mqttClient.subscribe('kpmqqt-lims-data-top2', (message) => {
          //   this.top2.push(message);
          // });
        })
        .catch(err => {
          console.error('MQTT connection failed:', err);
        });
    },
    publishMessage() {
      if (this.mqttClient) {
        this.mqttClient.publish('kpmqqt-lims-data-top1', 'Hello from 測試Vue2!');
        // this.mqttClient.publish('kpmqqt-lims-data-top2', 'Hello from 測試!');
      } else {
        console.error('MQTT client not initialized');
      }
    }
  },
  beforeDestroy() {
    if (this.mqttClient) {
      this.mqttClient.disconnect();
    }
  }
};
</script>

四、或者直接在頁面使用

<template>
  <div>
    <h3>接收的 MQTT 消息:</h3>
    <ul>
      <li v-for="(msg, index) in messages" :key="index">
        {{ msg.topic }}: {{ msg.content }}
      </li>
    </ul>
  </div>
</template>

<script>
import mqtt from 'mqtt';

export default {
  data() {
    return {
      client: null,
      messages: []
    };
  },
  mounted() {
    this.initMQTT();
  },
  methods: {
    initMQTT() {
      const options = {
        clientId: 'vue-client_' + Math.random().toString(16).substr(2, 8),
        clean: true,
        connectTimeout: 4000
      };
      const url="ws://"+"你的地址"+":8083/mqtt"  //例如ws://172.18.14.167:8083/mqtt
      this.client = mqtt.connect(url, options);

      this.client.on('connect', (e) => {
        this.subscribeTopics();
      });

      this.client.on('message', (topic, payload) => {
        this.messages.push({
          topic: topic,
          content: payload.toString()
        });
      });
    },
    subscribeTopics() {
      this.client.subscribe(['sensor/#'], { qos: 1 });
    }
  },
  beforeDestroy() {
    if (this.client) {
      this.client.end();
    }
  }
};
</script>

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue中使用event.target.value踩坑記錄

    vue中使用event.target.value踩坑記錄

    這篇文章主要介紹了vue中使用event.target.value踩坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 詳解Vue如何進(jìn)行表單聯(lián)動與級聯(lián)選擇

    詳解Vue如何進(jìn)行表單聯(lián)動與級聯(lián)選擇

    表單聯(lián)動和級聯(lián)選擇是Vue.js中常見的功能,在下面的文章中,我們將討論如何在Vue.js中實現(xiàn)表單聯(lián)動和級聯(lián)選擇,感興趣的小伙伴可以了解一下
    2023-06-06
  • Vue實現(xiàn)導(dǎo)航欄菜單

    Vue實現(xiàn)導(dǎo)航欄菜單

    這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)導(dǎo)航欄菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • vue實現(xiàn)拖拽的簡單案例 不超出可視區(qū)域

    vue實現(xiàn)拖拽的簡單案例 不超出可視區(qū)域

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)拖拽的簡單案例,不超出可視區(qū)域,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Vue項目如何設(shè)置反向代理和cookie設(shè)置問題

    Vue項目如何設(shè)置反向代理和cookie設(shè)置問題

    這篇文章主要介紹了Vue項目如何設(shè)置反向代理和cookie設(shè)置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue中$forceUpdate()的使用方式

    Vue中$forceUpdate()的使用方式

    這篇文章主要介紹了Vue中$forceUpdate()的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue實現(xiàn)計步器功能

    vue實現(xiàn)計步器功能

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)計步器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • vue中使用svg畫路徑圖的詳細(xì)介紹

    vue中使用svg畫路徑圖的詳細(xì)介紹

    這篇文章主要介紹了vue中使用svg畫路徑圖的相關(guān)知識,在這大家需要注意svg中不能使用html標(biāo)簽,例如div,img等,因此在svg中一般使用image標(biāo)簽放置圖片,text圖片放置文本內(nèi)容,詳細(xì)代碼跟隨小編一起看看吧
    2022-04-04
  • 解決在vue+webpack開發(fā)中出現(xiàn)兩個或多個菜單公用一個組件問題

    解決在vue+webpack開發(fā)中出現(xiàn)兩個或多個菜單公用一個組件問題

    這篇文章主要介紹了在vue+webpack實際開發(fā)中出現(xiàn)兩個或多個菜單公用一個組件的解決方案,需要的朋友可以參考下
    2017-11-11
  • Vue filter 過濾當(dāng)前時間 實現(xiàn)實時更新效果

    Vue filter 過濾當(dāng)前時間 實現(xiàn)實時更新效果

    這篇文章主要介紹了Vue filter 過濾當(dāng)前時間 實現(xiàn)實時更新效果,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12

最新評論