【问题标题】:How to prevent a callback in the Twitter stream from firing with each new tweet?如何防止 Twitter 流中的回调在每条新推文中触发?
【发布时间】:2014-04-19 20:18:05
【问题描述】:

我的应用会筛选 Twitter 流中的匹配字符串。当它找到匹配时,它应该增加一个计数器并通过 socket.io 将新的计数发送给客户端。不幸的是,socket.io 是在每条推文中发出的,而不是每场比赛。它的发射频率比必要的多,导致浏览器问题。如何防止回调被 Twitter 流中的每个新事件调用?

这是我的 twitter 流代码:

// Compares watchList (NYT Keywords) to Twitter stream, counts every mention
exports.keywordStream = function(callback) {
  exports.initializeFeed().then(function(watchList) {

  // enter the stream
    t.stream('statuses/filter', { track: watchKeywords }, function(stream) {

    // read twitter firehose ('data') for incoming tweets.
      stream.on('data', function(tweet) {
        var tweetText = tweet.text.toLowerCase();

        // compare the text of each tweet to each NYT keyword in the watchList
        _.each(watchKeywords, function(e) {

          // if the keyword exists in the tweet, += 1
          if (tweetText.indexOf(e.toLowerCase()) !== -1) {
            watchList.keywords[e] += 1;
            watchList.total += 1;
          }

        });
        callback(watchList);
      });
    });
  });
 };

这是我在 app.js 中的代码。只要有匹配的推文,我希望它发出。

io.sockets.on('connection', function(socket) {
  // console.log("client connected");
});

firehose.keywordStream(function(watchList) {
  // STREAMING . . .
  setInterval(function() {
    io.sockets.emit('watchUpdate', {'watchList': watchList});
  }, 1000/60);
});

这只是我的视图文件中的一个 sn-p:

<script>
 var socket = io.connect(window.location.hostname);
 console.log('hello world')
   socket.on('watchUpdate', function(data) {
   console.log('chirp')

大约 20 秒后,我的浏览器被锁定。它看起来像这样:

写完这个问题后,我意识到处理更新后的 watchList 的回调函数位于 twitter 流中。无论推文是否命中 IF 语句,似乎都会在每条推文中调用回调。如果我将回调放在 IF 语句中,回调仍然会在每条推文中被调用。

为有关 node.js 中回调性质的任何见解、提示或 cmets 欢呼。

【问题讨论】:

    标签: node.js twitter asynchronous callback socket.io


    【解决方案1】:

    我的问题是我在 setInterval 中嵌套了发射。 setInterval 与每个回调一起堆叠。如果删除 setInterval(),io.sockets.emit 将按预期执行。

    // app.js
    io.sockets.on('connection', function(socket) {
      // console.log("client connected");
    });
    
    firehose.keywordStream(function(watchList) {
      io.sockets.emit('watchUpdate', {'watchList': watchList});
    });
    

    最后一点是将我的回调放在 IF 语句中,以确保它仅在匹配的关键字上触发。

     // twitter firehose
     exports.keywordStream = function(callback) {
       exports.initializeFeed().then(function(watchList) {
    
         // enter the stream
         t.stream('statuses/filter', { track: watchKeywords }, function(stream) {
    
           // read twitter firehose ('data') for incoming tweets.
           stream.on('data', function(tweet) {
             var tweetText = tweet.text.toLowerCase();
    
             // compare the text of each tweet to each NYT keyword in the watchList
             _.each(watchKeywords, function(e) {
    
               // if the keyword exists in the tweet, += 1
               if (tweetText.indexOf(e.toLowerCase()) !== -1) {
                 watchList.keywords[e] += 1;
                 watchList.total += 1;
                 callback(watchList);
               }
             });
           });
         });
       });
     };
    

    通过这两项调整,浏览器可以接收到匹配推文的实时更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多