From 2a47951e46088ac3d43176d151fadfb4795ae44e Mon Sep 17 00:00:00 2001 From: dceejay Date: Mon, 16 Mar 2015 17:07:46 +0000 Subject: [PATCH] make sure MQTT msg has a topic --- nodes/core/io/10-mqtt.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/nodes/core/io/10-mqtt.js b/nodes/core/io/10-mqtt.js index 4e65bd39b..9085505c7 100644 --- a/nodes/core/io/10-mqtt.js +++ b/nodes/core/io/10-mqtt.js @@ -45,21 +45,26 @@ module.exports = function(RED) { this.status({fill:"red",shape:"ring",text:"disconnected"}); this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password); var node = this; - this.client.subscribe(this.topic,2,function(topic,payload,qos,retain) { - if (isUtf8(payload)) { payload = payload.toString(); } - var msg = {topic:topic,payload:payload,qos:qos,retain:retain}; - if ((node.brokerConfig.broker === "localhost")||(node.brokerConfig.broker === "127.0.0.1")) { - msg._topic = topic; - } - node.send(msg); - }); - this.client.on("connectionlost",function() { - node.status({fill:"red",shape:"ring",text:"disconnected"}); - }); - this.client.on("connect",function() { - node.status({fill:"green",shape:"dot",text:"connected"}); - }); - this.client.connect(); + if (this.topic) { + this.client.subscribe(this.topic,2,function(topic,payload,qos,retain) { + if (isUtf8(payload)) { payload = payload.toString(); } + var msg = {topic:topic,payload:payload,qos:qos,retain:retain}; + if ((node.brokerConfig.broker === "localhost")||(node.brokerConfig.broker === "127.0.0.1")) { + msg._topic = topic; + } + node.send(msg); + }); + this.client.on("connectionlost",function() { + node.status({fill:"red",shape:"ring",text:"disconnected"}); + }); + this.client.on("connect",function() { + node.status({fill:"green",shape:"dot",text:"connected"}); + }); + this.client.connect(); + } + else { + this.error("topic not defined"); + } } else { this.error("missing broker configuration"); }