Blame view

src/components/utils/mqtt.js 2.83 KB
de70de77   Zhang Zhuo   更新使用小功能 -张卓
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  import mqtt_module from 'mqtt'
  
  const Mqtt = function (url) {
      this.mqtt = mqtt_module
      this.random = getGuid32;
      this.clientId = getGuid32();
      let options = {
          clientId: this.clientId,
          username: 'test',
          password: 'test',
          connectTimeout: 100,
          keepalive: 100
      }
      this.client = this.mqtt.connect("ws://" + url + "/mqtt", options)
      // 重连次数 超5次就算了
      this.reconnectNum = 0
      // 连接
      this.link = function () {
          return new Promise((resolve, reject) => {
              this.client.on('connect', e => {
                  console.log('-----------------------链接成功-----------------------')
                  resolve(this.client)
              })
              this.client.on('reconnect', error => {
                  this.reconnectNum++
                  if (this.reconnectNum >= 10) this.client.end(true)
                  console.log('正在重连:', error)
              })
              this.client.on('error', error => {
                  console.log('订阅失败', error)
              })
          })
      }
      this.subscribe = function (topic, options) {
          this.client.subscribe(topic, options, (err) => {
              if (!err) {
                  console.log('-----------------------'+topic+'订阅成功-----------------------')
              } else {
                  throw new Error(err)
              }
          })
      }
      this.unsubscribe = function(topic, options) {
          this.client.unsubscribe(topic, options, (err) => {
              if (!err) {
                  console.log('-----------------------'+topic+'取消订阅成功-----------------------')
              } else {
                  throw new Error(err)
              }
          })
      }
      this.publish = function (topic, sendMsg, options) {
          this.client.publish(topic, JSON.stringify(sendMsg), options, (err, a) => {
              if (!err) {
                  console.log('-----------------------'+topic+'发送成功-----------------------')
              } else {
                  throw new Error(err)
              }
          })
      }
      this.message = function (callback) {
          this.client.on('message', (topic, message) => {
              let data = JSON.parse(message.toString())
              callback(data, topic)
          })
      }
      // 关闭
      this.close = function () {
          this.client.end(true)
      }
  }
  
  export default Mqtt
  
  function getGuid32() {
      var rt_str = String.fromCharCode(65 + Math.floor(Math.random() * 26));
      for (var i = 0; i < 31; ++i) {
          var num = Math.floor(Math.random() * (26 + 26 + 10));
          var ch_str;
          if (num < 10) {
              ch_str = num.toString();
          } else if (num < 10 + 26) {
              ch_str = String.fromCharCode(65 + num - 10);
          } else {
              ch_str = String.fromCharCode(97 + num - 10 - 26);
          }
          rt_str += ch_str;
      }
      return rt_str;
  }