无法销毁node.js中的Twitter流



我正在尝试将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();
}

相关内容

  • 没有找到相关文章

最新更新