我正在尝试将NPM twitter与Socket.io一起使用。 stream.destory()函数不起作用。
var util = require('util');
var twitter = require('twitter');
var twit = new twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
var currentTwitStream;
hashtags = [];
module.exports = function (io) {
io.sockets.on('connection', function (socket) {
console.log('A new user connected!');
socket.on('data', function(data){
hashtags.push('#' + data.hashtag);
hashtags.forEach(function(entry) {
console.log(entry);
});
if(currentTwitStream) currentTwitStream.destroy();
twit.stream('statuses/filter',{track:hashtags.join(',')},function(stream){
currentTwitStream = stream;
stream.on('data',function(data,err){
socket.emit();
console.log(data.text);
});
});
});
});
};
对于如何解决此问题的任何反馈,我将不胜感激。
最佳答案
以下代码对我有用,以销毁流。
var clientSteam = {};
clientSteam = stream;// After your stream.on('data',function(data,err){
//现在销毁流
if (Object.keys(clientSteam).length != 0)
setTimeout(closeStream, 1000);
function closeStream(){
console.log('Closing stream...');
clientSteam.destroy();
}
关于node.js - 无法销毁node.js中的Twitter流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26478603/