mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
parent
e6369820a9
commit
45913e5ee8
@ -184,71 +184,72 @@ module.exports = function(RED) {
|
|||||||
this.connect = function () {
|
this.connect = function () {
|
||||||
if (!node.connected && !node.connecting) {
|
if (!node.connected && !node.connecting) {
|
||||||
node.connecting = true;
|
node.connecting = true;
|
||||||
node.client = mqtt.connect(node.brokerurl ,node.options);
|
console.log("going for connect");
|
||||||
node.client.setMaxListeners(0);
|
try {
|
||||||
// Register successful connect or reconnect handler
|
node.client = mqtt.connect(node.brokerurl ,node.options);
|
||||||
node.client.on('connect', function () {
|
node.client.setMaxListeners(0);
|
||||||
node.connecting = false;
|
// Register successful connect or reconnect handler
|
||||||
node.connected = true;
|
node.client.on('connect', function () {
|
||||||
node.log(RED._("mqtt.state.connected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
node.connecting = false;
|
||||||
for (var id in node.users) {
|
node.connected = true;
|
||||||
if (node.users.hasOwnProperty(id)) {
|
node.log(RED._("mqtt.state.connected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
||||||
node.users[id].status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Remove any existing listeners before resubscribing to avoid duplicates in the event of a re-connection
|
|
||||||
node.client.removeAllListeners('message');
|
|
||||||
|
|
||||||
// Re-subscribe to stored topics
|
|
||||||
for (var s in node.subscriptions) {
|
|
||||||
if (node.subscriptions.hasOwnProperty(s)) {
|
|
||||||
var topic = s;
|
|
||||||
var qos = 0;
|
|
||||||
for (var r in node.subscriptions[s]) {
|
|
||||||
if (node.subscriptions[s].hasOwnProperty(r)) {
|
|
||||||
qos = Math.max(qos,node.subscriptions[s][r].qos);
|
|
||||||
node.client.on('message',node.subscriptions[s][r].handler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var options = {qos: qos};
|
|
||||||
node.client.subscribe(topic, options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send any birth message
|
|
||||||
if (node.birthMessage) {
|
|
||||||
node.publish(node.birthMessage);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
node.client.on("reconnect", function() {
|
|
||||||
for (var id in node.users) {
|
|
||||||
if (node.users.hasOwnProperty(id)) {
|
|
||||||
node.users[id].status({fill:"yellow",shape:"ring",text:"node-red:common.status.connecting"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// Register disconnect handlers
|
|
||||||
node.client.on('close', function () {
|
|
||||||
if (node.connected) {
|
|
||||||
node.connected = false;
|
|
||||||
node.log(RED._("mqtt.state.disconnected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
|
||||||
for (var id in node.users) {
|
for (var id in node.users) {
|
||||||
if (node.users.hasOwnProperty(id)) {
|
if (node.users.hasOwnProperty(id)) {
|
||||||
node.users[id].status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
|
node.users[id].status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (node.connecting) {
|
// Remove any existing listeners before resubscribing to avoid duplicates in the event of a re-connection
|
||||||
node.log(RED._("mqtt.state.connect-failed",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
node.client.removeAllListeners('message');
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Register connect error handler
|
// Re-subscribe to stored topics
|
||||||
node.client.on('error', function (error) {
|
for (var s in node.subscriptions) {
|
||||||
if (node.connecting) {
|
if (node.subscriptions.hasOwnProperty(s)) {
|
||||||
node.client.end();
|
var topic = s;
|
||||||
node.connecting = false;
|
var qos = 0;
|
||||||
}
|
for (var r in node.subscriptions[s]) {
|
||||||
});
|
if (node.subscriptions[s].hasOwnProperty(r)) {
|
||||||
|
qos = Math.max(qos,node.subscriptions[s][r].qos);
|
||||||
|
node.client.on('message',node.subscriptions[s][r].handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var options = {qos: qos};
|
||||||
|
node.client.subscribe(topic, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send any birth message
|
||||||
|
if (node.birthMessage) {
|
||||||
|
node.publish(node.birthMessage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
node.client.on("reconnect", function() {
|
||||||
|
for (var id in node.users) {
|
||||||
|
if (node.users.hasOwnProperty(id)) {
|
||||||
|
node.users[id].status({fill:"yellow",shape:"ring",text:"node-red:common.status.connecting"});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// Register disconnect handlers
|
||||||
|
node.client.on('close', function () {
|
||||||
|
if (node.connected) {
|
||||||
|
node.connected = false;
|
||||||
|
node.log(RED._("mqtt.state.disconnected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
||||||
|
for (var id in node.users) {
|
||||||
|
if (node.users.hasOwnProperty(id)) {
|
||||||
|
node.users[id].status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (node.connecting) {
|
||||||
|
node.log(RED._("mqtt.state.connect-failed",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register connect error handler
|
||||||
|
// The client's own reconnect logic will take care of errors
|
||||||
|
node.client.on('error', function (error) {});
|
||||||
|
}catch(err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user