Skip to content

Commit

Permalink
Fix frontend not working when MQTT broker is offline. nurikk/zigbee2m…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jun 22, 2021
1 parent ac85596 commit 0c45812
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/extension/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Frontend extends Extension {
this.onRequest = this.onRequest.bind(this);
this.onUpgrade = this.onUpgrade.bind(this);
this.mqttBaseTopic = settings.get().mqtt.base_topic;
this.onMQTTPublishedMessage = this.onMQTTPublishedMessage.bind(this);
this.mqtt.on('publishedMessage', this.onMQTTPublishedMessage);
this.onMQTTPublishMessage = this.onMQTTPublishMessage.bind(this);
this.mqtt.on('publishMessage', this.onMQTTPublishMessage);
this.onWebSocketConnection = this.onWebSocketConnection.bind(this);
this.server = http.createServer(this.onRequest);
this.server.on('upgrade', this.onUpgrade);
Expand Down Expand Up @@ -106,7 +106,7 @@ class Frontend extends Extension {
}
}

onMQTTPublishedMessage(data) {
onMQTTPublishMessage(data) {
let {topic, payload, options} = data;
if (topic.startsWith(`${this.mqttBaseTopic}/`)) {
// Send topic without base_topic
Expand Down
3 changes: 2 additions & 1 deletion lib/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class MQTT extends events.EventEmitter {
this.publishedTopics.add(topic);
}

this.emit('publishMessage', {topic, payload, options});

if (!this.isConnected()) {
if (!skipLog) {
logger.error(`Not connected to MQTT server!`);
Expand All @@ -135,7 +137,6 @@ class MQTT extends events.EventEmitter {

return new Promise((resolve) => {
this.client.publish(topic, payload, actualOptions, () => {
this.emit('publishedMessage', {topic, payload, options});
resolve();
});
});
Expand Down

0 comments on commit 0c45812

Please sign in to comment.