2013-10-24 36 views
0

我與此庫的工作:mTwitter如何訪問流中的數據?

我的問題是,當我想用​​流功能:

twit.stream.raw(
    'GET', 
    'https://stream.twitter.com/1.1/statuses/sample.json', 
    {delimited: 'length'}, 
    process.stdout 
); 

我不知道如何訪問產生process.stdout的JSON。

+0

它似乎是'node.js'的輸出函數(輸出到控制檯,這是'stdout'通常用於的)。你有沒有檢查[谷歌搜索「process.stdout」?](http://nodejs.org/api/process.html#process_process_stdout) – h2ooooooo

回答

1

您可以使用可寫入的流,從stream.Writable

var stream = require('stream'); 
var fs = require('fs'); 

// This is where we will be "writing" the twitter stream to. 
var writable = new stream.Writable(); 

// We listen for when the `pipe` method is called. I'm willing to bet that 
// `twit.stream.raw` pipes to stream to a writable stream. 
writable.on('pipe', function (src) { 

    // We listen for when data is being read. 
    src.on('data', function (data) { 
    // Everything should be in the `data` parameter. 
    }); 

    // Wrap things up when the reader is done. 
    src.on('end', function() { 
    // Do stuff when the stream ends. 
    }); 

}); 

twit.stream.raw(
    'GET', 
    'https://stream.twitter.com/1.1/statuses/sample.json', 
    {delimited: 'length'}, 

    // Instead of `process.stdout`, you would pipe to `writable`. 
    writable 
); 
0

我不確定你是否真的明白streaming是什麼意思。在node.js中,stream基本上是一個文件描述符。該示例使用process.stdout,但tcp套接字也是一個流,打開的文件也是一個流,管道也是一個流。

因此,一個streaming函數旨在將接收到的數據直接傳遞到流,而無需手動將數據從源複製到目標。顯然這意味着你不能訪問數據。想想像unix shell上的管道一樣流。這段代碼基本上是這樣做的:

twit_get | cat 

事實上,在節點上,您可以創建在純JS虛擬流。所以有可能獲得數據 - 你只需要實現一個流。查看流API的節點文檔:http://nodejs.org/api/stream.html