@burgrp/mqtt-mtl 1.0.3 → 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 +30 -29
- package/package.json +1 -1
package/mqtt-mtl.js
CHANGED
|
@@ -7,58 +7,59 @@ 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
|
+
listeners: {},
|
|
15
|
+
nextListenerId: 1,
|
|
14
16
|
|
|
15
17
|
publish(topic, message) {
|
|
16
18
|
this.connection.publish(topic, message);
|
|
17
19
|
},
|
|
18
20
|
|
|
19
21
|
subscribe(topic, listener) {
|
|
20
|
-
this.connection.subscribe(topic);
|
|
21
|
-
|
|
22
|
-
let parsedTopic = topic.split("/");
|
|
23
22
|
|
|
24
23
|
let id = this.nextListenerId++;
|
|
25
24
|
|
|
26
|
-
this.listeners[id] =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
if (parsedTopic[i] === "#") {
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
matches = false;
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
if (matches) {
|
|
40
|
-
listener(actTopic, actMessage);
|
|
25
|
+
this.listeners[id] = listener;
|
|
26
|
+
|
|
27
|
+
this.connection.subscribe(topic, {
|
|
28
|
+
properties: {
|
|
29
|
+
subscriptionIdentifier: id
|
|
41
30
|
}
|
|
42
|
-
};
|
|
31
|
+
});
|
|
43
32
|
|
|
44
33
|
return id;
|
|
45
34
|
},
|
|
46
35
|
|
|
47
36
|
unsubscribe(listenerId) {
|
|
48
|
-
|
|
37
|
+
//TODO: unsubscribe from MQTT if it's last subscription in this.subscriptions[]
|
|
38
|
+
//delete this.listeners[listenerId];
|
|
39
|
+
throw new Error("Not implemented yet");
|
|
49
40
|
}
|
|
50
41
|
}
|
|
51
42
|
|
|
52
43
|
brokers[brokerUrl] = broker;
|
|
53
44
|
|
|
54
|
-
broker.connection.on("message", (topic, message) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
+
}
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
+
|
|
62
63
|
});
|
|
63
64
|
}
|
|
64
65
|
|