无法在node.js中销毁Twitter流

时间:2014-10-21 03:52:45

标签: node.js twitter socket.io twitter-streaming-api

我正在尝试使用Socket.io的NPM twitter。 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);
            });
          });
    });
  });
};

如果有任何关于如何解决这个问题的反馈,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下代码为我工作以销毁流。

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();
}