vue,mtqq消息传输

<template>
  <div>
    <el-button
      type="text"
      @click="sendMsg"
      style="font-size: 12px"
      icon="el-icon-upload2"
    >
      发送
    </el-button>
    <el-button
      type="text"
      @click="returnMsg"
      style="font-size: 12px"
      icon="el-icon-upload2"
    >
      返回
    </el-button>
  </div>
</template>

<script>
import mqtt from "@/utils/mqtt";
export default {
   
  mixins: [mqtt],
  components: {
   },
  data() {
   
    return {
   
      fileList: [],
    };
  },
  props: {
   },
  computed: {
   
    mqttRefreshAttachmentFieldTopic() {
   
      return `user/${
    this.userId}/ygg_view/tag_view_file_view_unzip_file`;
    },
  },
  created() {
   },
  methods: {
   
    sendMsg() {
   
      this.initMqtt();
      this.mqttConnectSuccess();
    },
    returnMsg() {
   
      let prex = this.mqttRefreshAttachmentFieldTopic;
      if (prex) {
   
        this.publishMsg(prex, {
   
          action: "refresh_attachment_field",
          att: {
   
            filedId: "2222",
          },
        });
      }
    },

    initMqtt() {
   
      this.onPrintNotify();
      this.doConnect(this.mqttRefreshAttachmentFieldTopic);
    },

    destroyed() {
   
      this.unSubscribe(this.mqttRefreshAttachmentFieldTopic);
    },

    mqttConnectSuccess() {
   
      this.doSubscribe(this.mqttRefreshAttachmentFieldTopic);
    },
    onMqttMessage(topic, message) {
   
      let obj = JSON.parse(message);
      let data = obj.body.data;
      if (topic != this.mqttRefreshAttachmentFieldTopic) {
   
        return;
      }
      if (data.action === "refresh_attachment_field") {
   
        this.onReadyPrintNotify();
      }
    },
    onPrintNotify() {
   
      this.$notify({
   
        title: "上传压缩包中...",
        message:
          "您可以进行其他操作,上传在后台进行。上传完成后,您会在消息里收到完成提醒。",
        iconClass: "el-icon-refresh",
        duration: 5000,
      });
    },
    onReadyPrintNotify() {
   
      this.$notify({
   
        title: "成功提示",
        message: "上传压缩包完成",
        duration: 0,
        type: "success",
      });
    },
  },
  mounted() {
   },
};
</script>

<style lang="scss" scoped>
::v-deep {
   
  .el-button {
   
    font-size: 12px;
  }
}
.upload-container {
   
  display: none;
}
</style>

mqtt.js

import {
    mapState, mapMutations, mapGetters } from "vuex";
import {
    uuid } from "@/zgg-core/utils";

export default {
   
  computed: {
   
    ...mapState("mqtt", ["mqttClient"]),
    ...mapGetters(["currentUser"]),
  },
  created() {
   
    this.mqttUuid = uuid();
  },
  destroyed() {
   
    this.deleteMqttMessage(this.mqttUuid);
  },
  methods: {
   
    ...mapMutations("mqtt", [
      "mqttConnect",
      "setMqttMessage",
      "deleteMqttMessage",
    ]),
    doConnect(topic) {
   
      this.setMqttMessage({
   
        uuid: this.mqttUuid,
        callback: this.onMqttMessage,
      });
      if (this.mqttClient) {
   
        this.doSubscribe(topic);
        // console.log(this.mqttClient.off)
        // this.mqttClient.on("message", this.onMqttMessage);
      } else {
   
        this.mqttConnect({
   
          mqttConnectSuccess: this.mqttConnectSuccess,
          // onMqttMessage: this.onMqttMessage,
          userId: this.currentUser.id,
        });
      }
    },
    unSubscribe(topic, callback) {
   
      if (typeof topic !== "string") {
   
        return;
      }

      let client = this.mqttClient;
      client.unsubscribe(
        topic,

        (err, a) => {
   
          if (!err) {
   
            client.off("message", this.onMqttMessage);
            console.log("取消订阅", topic);
            if (typeof callback === "function") {
   
              callback();
            }
          }
        }
      );
    },
    doSubscribe: function (topic) {
   
      if (typeof topic !== "string") {
   
        return;
      }

      let client = this.mqttClient;
      this.unSubscribe(topic, () => {
   
        client.subscribe(
          topic,
          {
   
            qos: 0,
          },
          (err, a) => {
   
            if (!err) {
   
              // client.on('message', this.onMqttMessage)
              console.log("订阅成功", topic);
            }
          }
        );
      });
    },
    publishMsg(topic, msg) {
   
      try {
   
        this.mqttClient.publish(topic, JSON.stringify({
    body: {
    data: msg } }));
        // alert(topic + "," + JSON.stringify(msg))
      } catch (error) {
   
        alert("publis error");
      }
    },
  },
};

相关推荐

  1. vue,mtqq消息传输

    2023-12-15 18:38:04       44 阅读
  2. RabbitMQ消息的可靠传输和防止消息丢失

    2023-12-15 18:38:04       7 阅读
  3. wpf 消息传递

    2023-12-15 18:38:04       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 18:38:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 18:38:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 18:38:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 18:38:04       20 阅读

热门阅读

  1. Linux 中安装Python3 的详细步骤

    2023-12-15 18:38:04       36 阅读
  2. angular hero学习

    2023-12-15 18:38:04       33 阅读
  3. (第21天)Oracle 数据泵常用参数和命令

    2023-12-15 18:38:04       29 阅读
  4. Vue 宝典之动画(transition)

    2023-12-15 18:38:04       42 阅读
  5. postman中Test断言介绍

    2023-12-15 18:38:04       35 阅读
  6. 算法训练营Day15

    2023-12-15 18:38:04       40 阅读
  7. 1.两数之和

    2023-12-15 18:38:04       36 阅读
  8. Makefile

    Makefile

    2023-12-15 18:38:04      33 阅读
  9. 2023数学建模黄河水沙监测数据分析思路

    2023-12-15 18:38:04       34 阅读