2013-10-24 37 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