使用不同的轨道关键字重新启动nodejs ntwitter twitter流 [英] Restarting nodejs ntwitter twitter stream with different track keywords
问题描述
var twitter = require('ntwitter');
// Configure twitter
var keywords = ['hello', 'world'];
twit.stream('statuses/filter', {'track':keywords.join(',')}, function(stream) {
stream.on('data', function (data) {
console.log(data);
});
stream.on('end', function (response) {
console.log("\n====================================================");
console.log("DESTROYING");
console.log("====================================================\n");
});
setTimeout(function(){
stream.destroy();
}, 60000);
});
我是 nodejs 的新手.阻止这种情况并使用一组不同的关键字重新开始的最佳方法是什么?
I'm new to nodejs. What is the best way to stop this and start it again with but a different set of keywords.
我可以销毁()流,然后创建一个新的.但无论如何,我可以在不断开连接的情况下更改曲目关键字吗?
I can destroy() the stream and then create a new one. But is there anyway I can just change the track keywords without disconnecting?
推荐答案
我是菜鸟,所以可能这种方式不太好,浪费资源,但我还是不知道如何检查,所以我会把它放在这里,希望有技术的人告诉我们它是可以的还是错误的,最重要的是,为什么?.
I'm quite noob, so maybe this way it's not a good one and it's wasting resources, but I still don't know how to check it, so I will drop it here and hope that someone skilled told us if it's OK or it's wrong, and the most important, why?.
方法是将 tw.stream 调用放在一个函数中,并使用您要跟踪的单词数组调用该函数.它将开始跟踪新词,并停止跟踪已删除的词:
The way is to put the tw.stream call, inside a function and call that function with the array of words you want to track. It will start tracking new words, and stop tracking removed words:
// Array to store the tracked words
var TwitWords = [];
// Tracker function
function TrackWords(array){
tw.stream('statuses/filter',{track:array},function(stream){
stream.on('data',function(data){
console.log(data.text);
});
});
}
// Add word
function AddTwitWord(word){
if(TwitWords.indexOf(word)==-1){
TwitWords.push(word);
TrackWords(TwitWords);
}
}
// Remove word
function RemoveTwitWord(word){
if(TwitWords.indexOf(word)!=-1){
TwitWords.splice(TwitWords.indexOf(word),1);
TrackWords(TwitWords);
}
}
我希望一切顺利,因为这是我找到的唯一方法.
I hope it's ok, because it's the only way I found.
这篇关于使用不同的轨道关键字重新启动nodejs ntwitter twitter流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!