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

clear twitter reconnect timeout to close #237

This commit is contained in:
Dave Conway-Jones 2016-09-30 08:54:55 +01:00
parent eeb1189092
commit 741d3db06b

View File

@ -193,12 +193,13 @@ module.exports = function(RED) {
try { try {
var thing = 'statuses/filter'; var thing = 'statuses/filter';
var tags = node.tags; var tags = node.tags;
var tout;
var st = { track: [tags] }; var st = { track: [tags] };
var setupStream = function() { var setupStream = function() {
if (node.active) { if (node.restart) {
twit.stream(thing, st, function(stream) { twit.stream(thing, st, function(stream) {
//console.log(st); //console.log("ST",st);
node.stream = stream; node.stream = stream;
stream.on('data', function(tweet) { stream.on('data', function(tweet) {
if (tweet.user !== undefined) { if (tweet.user !== undefined) {
@ -224,12 +225,14 @@ module.exports = function(RED) {
} else { } else {
node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc})); node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc}));
} }
setTimeout(setupStream,10000); if (node.restart) {
tout = setTimeout(setupStream(),15000);
}
}); });
stream.on('destroy', function (response) { stream.on('destroy', function (response) {
if (this.active) { if (node.restart) {
node.warn(RED._("twitter.errors.unexpectedend")); node.warn(RED._("twitter.errors.unexpectedend"));
setTimeout(setupStream,10000); tout = setTimeout(setupStream(),15000);
} }
}); });
}); });
@ -258,11 +261,17 @@ module.exports = function(RED) {
if (this.user === "false") { if (this.user === "false") {
node.on("input", function(msg) { node.on("input", function(msg) {
if (this.tags === '') { if (this.tags === '') {
if (this.stream) { this.stream.destroy(); } if (tout) { clearTimeout(tout); }
if (this.stream) {
this.restart = false;
node.stream.removeAllListeners();
this.stream.destroy();
}
if ((typeof msg.payload === "string") && (msg.payload !== "")) { if ((typeof msg.payload === "string") && (msg.payload !== "")) {
st = { track:[msg.payload] }; st = { track:[msg.payload] };
tags = msg.payload; tags = msg.payload;
node.status({fill:"green", shape:"ring", text:tags}); node.status({fill:"green", shape:"ring", text:tags});
this.restart = true;
setupStream(); setupStream();
} }
else { else {
@ -292,7 +301,8 @@ module.exports = function(RED) {
this.on('close', function() { this.on('close', function() {
if (this.stream) { if (this.stream) {
this.active = false; this.restart = false;
node.stream.removeAllListeners();
this.stream.destroy(); this.stream.destroy();
} }
if (this.poll_ids) { if (this.poll_ids) {