mqtt

// 开源sdk地址:https://github.com/mqttjs/MQTT.js
import { uuid } from "vue-uuid";
const mqtt = require("mqtt"); // uuid object is also exported to things

class Client {
  constructor() {
    this.endPoint = "..";
    this.accessKey = "";
    this.secretKey = "xxxx";
    this.groupId = "GID_123@@@";
    this.UUID = uuid.v1();

    this.clientId = this.groupId + this.UUID;
    this.instanceId = "";
    this.topic = "";

    this.token =
    this.username = "Token|" + this.accessKey + "|" + this.instanceId;
    this.password = "RW|" + this.token;
    this.options = {
      username: this.username,
      password: this.password,
      clientId: this.clientId,
      cleanSession: true,
      keepalive: 90,
      connectTimeout: 3000
    };
    this.client = null;
    this.messageMap = {};
  }

  // 建立链接
  MQTTconnect() {
    const client = mqtt.connect("tcp://" + this.endPoint + ":80", this.options);
    this.client = client;
    client.on("connect", () => {
      this.onConnect();
    });
    client.on("message", (topic, message) => this.onMessage(topic, message));
    client.on("error", error => {
      this.onError(error);
    });
  }

  // 建立连接成功
  onConnect() {
    this.onReceiver({ Event: "open" });
  }

  // 收到消息
  onMessage(topic, message) {
    // console.log(topic, message);
    const mqttdata = JSON.parse(this.unzip(message));
    if (mqttdata.code === 0) {
      const params = {
        topic: topic,
        data: mqttdata.data
      };
      // console.log(params);

      this.onReceiver({ Event: "message", Data: params });
    }
  }

  // 订阅消息
  subscibe(topic) {
    this.client.subscribe(topic, { qos: 2 });
  }

  // 取消订阅
  unsubscibe(topic) {
    console.log("取消订阅" + topic);

    this.client.unsubscribe(topic);
  }

  // 关闭mqtt
  end() {
    this.client.end({ force: true });
  }

  // 自定义监听
  on(name, handler) {
    this.messageMap[name] = handler;
  }

  // 错误信息
  onError(error) {
    console.log(error);
  }

  // 回调数据
  onReceiver(data) {
    const callback = this.messageMap[data.Event];
    if (callback) callback(data.Data);
  }

  // 解压方法
  unzip(b64Data) {
    var strData = atob(b64Data);
    // Convert binary string to character-number array
    var charData = strData.split("").map(function(x) {
      return x.charCodeAt(0);
    });
    // Turn number array into byte-array
    var binData = new Uint8Array(charData);
    // // unzip
    // eslint-disable-next-line no-undef
    var data = pako.inflate(binData);
    // Convert gunzipped byteArray back to ascii string:
    // strData   = String.fromCharCode.apply(null, new Uint16Array(data));
    var array = new Uint16Array(data);
    var res = "";
    var chunk = 8 * 1024;
    var i;
    for (i = 0; i < array.length / chunk; i++) {
      res += String.fromCharCode.apply(
        null,
        array.slice(i * chunk, (i + 1) * chunk)
      );
    }
    res += String.fromCharCode.apply(null, array.slice(i * chunk));

    strData = res;
    return strData;
  }
}
export default Client;

你可能感兴趣的:(mqtt)