我正在使用这个库:mTwitter
我的问题是,当我想使用流功能时:
twit.stream.raw(
'GET',
'https://stream.twitter.com/1.1/statuses/sample.json',
{delimited: 'length'},
process.stdout
);
我不知道如何访问生成process.stdout
.
我正在使用这个库:mTwitter
我的问题是,当我想使用流功能时:
twit.stream.raw(
'GET',
'https://stream.twitter.com/1.1/statuses/sample.json',
{delimited: 'length'},
process.stdout
);
我不知道如何访问生成process.stdout
.
您可以使用可写流,来自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
);
我不确定你是否真的理解这个词streaming
在这里的意思。在 node.js 中,astream
基本上是一个文件描述符。该示例使用process.stdout
但 tcp 套接字也是流,打开的文件也是流,管道也是流。
因此,一个streaming
函数旨在将接收到的数据直接传递到流中,而无需您手动将数据从源复制到目标。显然,这意味着您无法访问数据。想想像 unix shell 上的管道一样流式传输。那段代码基本上是这样做的:
twit_get | cat
其实在node中,可以用纯js创建虚拟流。所以有可能获取数据——你只需要实现一个流。查看流API的节点文档:http ://nodejs.org/api/stream.html