vue中运用mqtt服务

1.安装mqtt插件
npm install mqtt --save

2.打开所给地址,将代码下载到本地

https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js

3.引入。

import "./mqttws31.min";

4.函数中调用

created() {
    // 连接服务器并注册连接成功处理事件
    this.clientMqtt = this.client();
    this.clientMqtt.connect(this.options());
    this.clientMqtt.onMessageArrived = this.onMessageArrived;
    // 注册连接断开处理事件
    this.clientMqtt.onConnectionLost = this.onConnectionLost;
  },
methods: {
    client: function () {
       // 下面参数都是后端给定的
      return new Paho.MQTT.Client(this.hostname, this.port, this.clientId);
    },
    // mqtt配置项
    options: function () {
      let options = {
        invocationContext: {
          host: this.hostname,
          port: this.port,
          path: this.clientMqtt.path,
          clientId: this.clientId,
        },
        timeout: 5000,
        keepAliveInterval: 20,
        cleanSession: false,
        useSSL: false,
        userName: "demo",    //任意名字即可
        password: this.token, //登录后获取的token值
        onSuccess: this.onConnect,
        onFailure: function (e) {
          console.log(e);
        },
      };
      return options;
    },
    // mqtt连接成功
    onConnect: function () {
      console.log("onConnected");
    },
    // mqtt接收消息
    onMessageArrived(message) {
      let messageJSON = JSON.parse(message.payloadString);
      if (this.clientid == messageJSON.sendRegId) {
        this.msgchild.saveMsgMap(
          messageJSON.vContent,
          messageJSON.sendRegId,
          false,
          messageJSON
        );
      } else {
        for (var j = 0; j < this.chatList.length; j++) {
          let clientId = this.chatList[j].clientId;
          if (clientId == messageJSON.sendRegId) {
            this.chatList[j].readCount += 1;
            break;
          }
        }
        this.PushMsgMap(
          messageJSON.vContent,
          messageJSON.sendRegId,
          false,
          messageJSON
        );
      }
      this.chatList = this.chatList.sort(this.msgchild.sortTime("readCount"));
      this.onScroll();
    },
    // mqtt断开事件
    onConnectionLost: function (responseObject) {
      if (responseObject.errorCode !== 0) {
        console.log("onConnectionLost:" + responseObject.errorMessage);
        console.log("连接已断开");
      }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue使用MQTT,你可以使用MQTT.js库来实现。以下是一些基本步骤: 1. 安装MQTT.js库:在你的Vue项目,使用npm或yarn安装mqtt库: ``` npm install mqtt ``` 或 ``` yarn add mqtt ``` 2. 导入MQTT库:在你的Vue组件,导入mqtt库: ```javascript import mqtt from 'mqtt'; ``` 3. 创建MQTT客户端:在Vue组件,创建一个MQTT客户端实例,并连接到MQTT代理: ```javascript const client = mqtt.connect('mqtt://mqtt.example.com'); // 替换为你的MQTT代理地址 ``` 4. 监听MQTT连接状态:可以监听MQTT客户端的连接状态,以便在连接成功或失败时进行处理: ```javascript client.on('connect', function () { console.log('Connected to MQTT broker'); }); client.on('error', function (error) { console.error('MQTT connection error:', error); }); ``` 5. 订阅和接收消息:可以订阅一个或多个主题,并在接收到消息时进行处理: ```javascript client.subscribe('topic1'); client.on('message', function (topic, message) { console.log('Received message:', message.toString()); }); ``` 6. 发布消息:可以使用`client.publish()`方法发布消息到指定的主题: ```javascript client.publish('topic1', 'Hello MQTT'); ``` 这些是基本的使用步骤,你可以根据需要进行扩展和自定义。记得在不需要时断开MQTT连接: ```javascript client.end(); ``` 请注意,这只是一个简单的示例,实际使用时可能需要处理更多的逻辑和错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值