连续两个以上的流?

时间:2013-03-08 13:20:45

标签: node.js

我无法连续两个流在node.js中工作。请参阅以下代码:

var Stream = require('stream');

var stream1 = new Stream;
stream1.readable = true;

var stream2 = new Stream;
stream2.readable = stream2.writable = true;

var stream3 = new Stream;
stream3.readable = stream3.writeable = true;

var i = 0;

stream2.write = function(data){this.emit('data', data)};
stream3.write = function(data){this.emit('data', data)};

setInterval((function() {
stream1.emit('data', String(i++));
}), 100);

//works:
stream1.pipe(stream2).pipe(process.stdout);

//does not work - why???
//stream1.pipe(stream2).pipe(stream3).pipe(process.stdout);

如何连续使用两个流?

2 个答案:

答案 0 :(得分:1)

你会觉得非常愚蠢,这是一个拼写错误:

stream2.readable = stream2.writable = true;
stream3.readable = stream3.writeable = true;  

查看差异,e中的额外stream3.writable

答案 1 :(得分:0)

尝试async(或来自nmpjs.com) - 最流行的Node.js模块,用于处理带有示例和wiki的异步JavaScript。