Nodejs可读流推送使用

时间:2014-02-13 09:15:12

标签: node.js stream

我在命令中使用node运行一些代码; 这样的代码是错误的:

> var rs = new require('stream').Readable();
> rs.push("123");rs.push(null); // two pushes are in the same row;

但这是错误:

> var rs = new require('stream').Readable();
> rs.push("123");  // I went them are not in the same row;
// then get a error, like :
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: not implemented
    at Readable._read (_stream_readable.js:446:22)
    at Readable.read (_stream_readable.js:320:10)
    at maybeReadMore_ (_stream_readable.js:431:12)
    at _stream_readable.js:422:7
    at process._tickCallback (node.js:415:13)

我需要这样的代码:

var rs = new require('stream').Readable();
rs.pipe(someWriteAbleStream)
// some time later
rs.push(somedata);
// some time late
rs.push(somedata);
// ...
rs.push(null);

感谢;

1 个答案:

答案 0 :(得分:2)

因为,如果你没有rs.push(null),那么cache为null,所以rs调用._read()来读取。

如果rs.push(null),则表示已完成。