如何使用ntwitter停止和重新启动Twitterstream
我是新来的node.js所以请原谅简单的问题。 我正在写一些标准的东西:一个使用ntwitter前往Twitter的其余API,并保存在MySQL的东西。 我遇到的麻烦是使用ntwitter断开连接并重新连接到twitter。
我检查了一个类似的问题,但我仍然没有得到答复: 重新启动nodejs ntwitter twitterstream与不同的轨道关键字
问题有两个:
1)如何检查twit.stream
是否存在
2)如何杀死它/手动断开twitter
我正在使用destroy
但似乎并没有工作。 代码的相关部分是:
if (values[0] == 'newStatusAdded') { console.log('Need to stop twitter listener'); //check if twitter stream is running if (twit.stream) { twit.stream.destroy; debugger; console.log('Stopped twitter listener'); }
谢谢….
我认为你有这样的设置在你的代码:
twit.stream('statuses/sample', function(stream) { stream.on('data', function (data) { console.log(data); }); });
在callback中保存streamvariables,例如在stream.on()
:
twit.currentTwitStream = stream;
这只是一个build议。 也许你有一个中央configuration对象,这将是一个更好的地方为当前的stream对象。 使用currentTwitStream
你可以做到这一点:
-
currentTwitStream.readable
在错误或closures后是错误的。 -
currentTwitStream.destroy()
破坏stream。
我也面临这个问题。 什么最终为我工作stream.stop()
。
.destroy()
.disconnect()
等带来了各种各样的错误,如
.destroy
没有定义。