@burgrp/mqtt-mtl 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/mqtt-mtl.js +27 -42
- package/package.json +1 -1
package/mqtt-mtl.js
CHANGED
|
@@ -7,11 +7,12 @@ module.exports = brokerUrl => {
|
|
|
7
7
|
if (!brokers[brokerUrl]) {
|
|
8
8
|
let broker = {
|
|
9
9
|
|
|
10
|
-
connection: mqtt.connect(brokerUrl
|
|
10
|
+
connection: mqtt.connect(brokerUrl, {
|
|
11
|
+
protocolVersion: 5
|
|
12
|
+
}),
|
|
11
13
|
|
|
12
|
-
listeners:
|
|
13
|
-
nextListenerId:
|
|
14
|
-
subscriptions: {},
|
|
14
|
+
listeners: {},
|
|
15
|
+
nextListenerId: 1,
|
|
15
16
|
|
|
16
17
|
publish(topic, message) {
|
|
17
18
|
this.connection.publish(topic, message);
|
|
@@ -19,41 +20,15 @@ module.exports = brokerUrl => {
|
|
|
19
20
|
|
|
20
21
|
subscribe(topic, listener) {
|
|
21
22
|
|
|
22
|
-
if (!(topic instanceof Object)) {
|
|
23
|
-
topic = {
|
|
24
|
-
strict: topic,
|
|
25
|
-
loose: topic
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (!this.subscriptions[topic.loose]) {
|
|
30
|
-
this.connection.subscribe(topic.loose);
|
|
31
|
-
this.subscriptions[topic.loose] = 1;
|
|
32
|
-
} else {
|
|
33
|
-
this.subscriptions[topic.loose]++;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let parsedTopic = topic.strict.split("/");
|
|
37
|
-
|
|
38
23
|
let id = this.nextListenerId++;
|
|
39
24
|
|
|
40
|
-
this.listeners[id] =
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
if (parsedTopic[i] === "#") {
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
50
|
-
matches = false;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
if (matches) {
|
|
54
|
-
listener(actTopic, actMessage);
|
|
25
|
+
this.listeners[id] = listener;
|
|
26
|
+
|
|
27
|
+
this.connection.subscribe(topic, {
|
|
28
|
+
properties: {
|
|
29
|
+
subscriptionIdentifier: id
|
|
55
30
|
}
|
|
56
|
-
};
|
|
31
|
+
});
|
|
57
32
|
|
|
58
33
|
return id;
|
|
59
34
|
},
|
|
@@ -67,14 +42,24 @@ module.exports = brokerUrl => {
|
|
|
67
42
|
|
|
68
43
|
brokers[brokerUrl] = broker;
|
|
69
44
|
|
|
70
|
-
broker.connection.on("message", (topic, message) => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
45
|
+
broker.connection.on("message", (topic, message, packet) => {
|
|
46
|
+
|
|
47
|
+
let ids = (packet.properties && packet.properties.subscriptionIdentifier) || [];
|
|
48
|
+
if (!(ids instanceof Array)) {
|
|
49
|
+
ids = [ids];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (let id of ids) {
|
|
53
|
+
let listener = broker.listeners[id];
|
|
54
|
+
if (listener) {
|
|
55
|
+
try {
|
|
56
|
+
listener(topic, message);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
console.error("Unhandled MQTT listener exception:", e);
|
|
59
|
+
}
|
|
76
60
|
}
|
|
77
61
|
}
|
|
62
|
+
|
|
78
63
|
});
|
|
79
64
|
}
|
|
80
65
|
|