support for multiple callbacks per topic started
This commit is contained in:
17
dist/mqttclient.js
vendored
17
dist/mqttclient.js
vendored
@ -13,8 +13,19 @@ class MqttClient {
|
||||
this._topicHandlers = [];
|
||||
}
|
||||
register(topic, callback) {
|
||||
this._topicHandlers.push({ topic, callback });
|
||||
log.info(`handler registered for topic ${topic}`);
|
||||
let done = false;
|
||||
for (let topicHandler of this._topicHandlers) {
|
||||
if (topicHandler.topic === topic) {
|
||||
topicHandler.callbacks.push(callback);
|
||||
done = true;
|
||||
log.info(`additional callback added for topic ${topic}`);
|
||||
}
|
||||
}
|
||||
if (!done) {
|
||||
let cbs = [callback];
|
||||
this._topicHandlers.push({ topic: topic, callbacks: cbs });
|
||||
log.info(`first callback added for topic ${topic}`);
|
||||
}
|
||||
}
|
||||
exec() {
|
||||
this._mqttClient = Mqtt.connect(this._mqttBrokerUrl);
|
||||
@ -30,7 +41,7 @@ class MqttClient {
|
||||
for (let topicHandler of this._topicHandlers) {
|
||||
if (this.topicMatch(topicHandler.topic, topic)) {
|
||||
log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`);
|
||||
topicHandler.callback(payload);
|
||||
// topicHandler.callback(payload)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user