1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add IRC server link check and retry after timeout

Fix for #430
This commit is contained in:
Dave C-J 2014-10-03 21:14:17 +01:00
parent 489c552dbe
commit 43f55c6038

View File

@ -24,6 +24,7 @@ module.exports = function(RED) {
this.server = n.server; this.server = n.server;
this.channel = n.channel; this.channel = n.channel;
this.nickname = n.nickname; this.nickname = n.nickname;
this.lastseen = 0;
this.ircclient = null; this.ircclient = null;
this.on("close", function() { this.on("close", function() {
if (this.ircclient != null) { if (this.ircclient != null) {
@ -44,15 +45,36 @@ module.exports = function(RED) {
if (node.serverConfig.ircclient == null) { if (node.serverConfig.ircclient == null) {
node.log("Connecting to "+node.serverConfig.server); node.log("Connecting to "+node.serverConfig.server);
node.status({fill:"grey",shape:"dot",text:"connecting"}); node.status({fill:"grey",shape:"dot",text:"connecting"});
node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname); node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname,{autoConnect:false,retryDelay:20000});
node.serverConfig.ircclient.addListener('error', function(message) { node.serverConfig.ircclient.addListener('error', function(message) {
node.log(JSON.stringify(message)); node.log(JSON.stringify(message));
}); });
node.serverConfig.ircclient.addListener('netError', function(message) {
node.log(JSON.stringify("NET "+message));
});
node.serverConfig.ircclient.addListener('connect', function() {
node.serverConfig.lastseen = Date.now();
});
node.serverConfig.ircclient.addListener('ping', function(server) {
node.serverConfig.lastseen = Date.now();
node.log("PING "+JSON.stringify(server));
});
node.recon = setInterval( function() {
//console.log("CHK ",(Date.now()-node.serverConfig.lastseen)/1000);
if ((Date.now()-node.serverConfig.lastseen) > 300000) { // if more than 5 mins since last seen
node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
}
if ((Date.now()-node.serverConfig.lastseen) > 603000) { // If more than 10 mins
node.serverConfig.ircclient.disconnect();
node.serverConfig.ircclient.connect();
node.log("Reconnect"); // then retry
}
node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
}, 60000); // check every 1 min
} }
else { node.status({text:""}); } else { node.status({text:""}); }
node.ircclient = node.serverConfig.ircclient; node.ircclient = node.serverConfig.ircclient;
node.ircclient.addListener('registered', function(message) { node.ircclient.addListener('registered', function(message) {
node.log(node.ircclient.nick+" ONLINE"); node.log(node.ircclient.nick+" ONLINE");
node.status({fill:"yellow",shape:"dot",text:"connected"}); node.status({fill:"yellow",shape:"dot",text:"connected"});
@ -103,7 +125,12 @@ module.exports = function(RED) {
var msg = { "payload": { "type": "names", "channel": channel, "names": nicks} }; var msg = { "payload": { "type": "names", "channel": channel, "names": nicks} };
node.send([null, msg]); node.send([null, msg]);
}); });
node.ircclient.addListener('raw', function (message) { // any message means we are alive
node.serverConfig.lastseen = Date.now();
});
node.on("close", function() {
if (node.recon) { clearInterval(node.recon); }
});
} }
RED.nodes.registerType("irc in",IrcInNode); RED.nodes.registerType("irc in",IrcInNode);
@ -119,10 +146,33 @@ module.exports = function(RED) {
if (node.serverConfig.ircclient == null) { if (node.serverConfig.ircclient == null) {
node.log("Connecting to "+node.serverConfig.server); node.log("Connecting to "+node.serverConfig.server);
node.status({fill:"grey",shape:"dot",text:"connecting"}); node.status({fill:"grey",shape:"dot",text:"connecting"});
node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname); node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname,{autoConnect:false,retryDelay:20000});
node.serverConfig.ircclient.addListener('error', function(message) { node.serverConfig.ircclient.addListener('error', function(message) {
node.log(JSON.stringify(message)); node.log(JSON.stringify(message));
}); });
node.serverConfig.ircclient.addListener('netError', function(message) {
node.log(JSON.stringify("NET "+message));
});
node.serverConfig.ircclient.addListener('connect', function() {
node.serverConfig.lastseen = Date.now();
});
node.serverConfig.ircclient.addListener('ping', function(server) {
node.serverConfig.lastseen = Date.now();
node.log("PING "+JSON.stringify(server));
});
node.recon = setInterval( function() {
//console.log("CHK ",(Date.now()-node.serverConfig.lastseen)/1000);
if ((Date.now()-node.serverConfig.lastseen) > 300000) { // if more than 5 mins since last seen
node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
}
if ((Date.now()-node.serverConfig.lastseen) > 603000) { // If more than 10 mins
node.serverConfig.ircclient.disconnect();
node.serverConfig.ircclient.connect();
console.log("Reconnect"); // then retry
}
node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
}, 60000); // check every 1 min
node.serverConfig.ircclient.connect();
} }
else { node.status({text:""}); } else { node.status({text:""}); }
node.ircclient = node.serverConfig.ircclient; node.ircclient = node.serverConfig.ircclient;
@ -136,6 +186,10 @@ module.exports = function(RED) {
}); });
}); });
node.ircclient.addListener('raw', function (message) { // any message received means we are alive
if (message.commandType === "reply") { node.serverConfig.lastseen = Date.now(); }
});
node.on("input", function(msg) { node.on("input", function(msg) {
if (Object.prototype.toString.call( msg.raw ) === '[object Array]') { if (Object.prototype.toString.call( msg.raw ) === '[object Array]') {
node.log("RAW command:"+msg.raw); node.log("RAW command:"+msg.raw);
@ -168,6 +222,10 @@ module.exports = function(RED) {
} }
} }
}); });
node.on("close", function() {
if (node.recon) { clearInterval(node.recon); }
});
} }
RED.nodes.registerType("irc out",IrcOutNode); RED.nodes.registerType("irc out",IrcOutNode);
} }