Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

mqtt.js 使用时,发起第一次连接不会返回packet,发起第二次时会返回第一次的的packet.

### 问题描述
mqtt.js 使用时,发起第一次连接不会返回packet,发起第二次时会返回第一次的的packet.
### 问题出现的环境背景及自己尝试过哪些方法

### 相关代码
粘贴代码文本(请勿用截图)

async triConnection(item, index) {
      const url = "mqtt://test.mosquitto.org";
      const option = {
        reconnectPeriod: 0,
        keepalive: 9000,
      };
      let client = await mqtt.connect(url, option);
      client.on("connect", () => {
        console.log("publish");
        console.log("conect", client);
        this.checkindex.push(index);
      });
      client.on("close", () => {
        console.log("close");
        let itemindex = this.checkindex.findIndex((e) => {
          return e == index;
        });
        this.checkindex.splice(itemindex, 1);
        console.log(this.checkindex);
      });
      client.on("packetreceive", (packetreceive) => {
        return console.log(packetreceive);
      });

### 你期待的结果是什么?实际看到的错误信息又是什么?
第一次点击发起连接 ,不会返回packet ,知道超时出发close事件
image
连着点击两次会触发连接成功的事件
image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...