[英]Stream in NodeJS
I need some help to understand how stream work in NodeJS 我需要一些帮助来了解NodeJS中的流如何工作
I explain, i need to write a module which call a UNIX process (with spawn
) and I want to redirect the stdout
of this process to a Readable Stream. 我解释一下,我需要编写一个调用UNIX进程的模块(使用spawn
),我想将此进程的stdout
重定向到可读流。
I want this behavior to exports
the Readable Stream and allow another module to read them. 我希望这种行为exports
可读流并允许另一个模块读取它们。
To do this, I have write a little piece of code : 为此,我写了一小段代码:
var spawn = require('child_process').spawn
var Duplex = require('stream').Duplex;
var stream = new Duplex;
var start = function() {
ps = spawn('mycmd', [/*... args ...*/]);
ps.stdout.pipe(stream);
};
exports.stream = stream;
exports.start = start;
But if I use this module I throw an exception which say that the stream doesn't implement the _read method. 但是如果我使用这个模块,我会抛出一个异常,说该流没有实现_read方法。
Can you help me with this problem ? 你能帮我解决这个问题吗?
Thanks in advance. 提前致谢。
[ EDIT ] I have try the solution of creating a Stream object, but that's doesnt work, here is the code: [ 编辑 ]我已经尝试了创建Stream对象的解决方案,但这不起作用,这里是代码:
var spawn = require('child_process').spawn;
var Stream = require('stream');
var ps = null;
var audio = new Stream;
audio.readable = audio.writable = true;
var start = function() {
if(ps == null) {
ps = spawn('mycmd', []);
ps.stdout.pipe(stream);
}
};
var stop = function() {
if(ps) {
ps.kill();
ps = null;
}
};
exports.stream = stream;
exports.start = start;
exports.stop = stop;
But when I try to listen the stream, I encount an new error : 但是当我尝试收听流时,我会输入一个新错误:
_stream_readable.js:583
var written = dest.write(chunk);
^
TypeError: Object #<Stream> has no method 'write'
Most of Node's Stream classes aren't meant to be used directly, but as the base of a custom type: 大多数Node的Stream类不是直接使用的,而是作为自定义类型的基础:
Note that
stream.Duplex
is an abstract class designed to be extended with an underlying implementation of the_read(size)
and_write(chunk, encoding, callback)
methods as you would with a Readable or Writable stream class. 请注意,stream.Duplex
是一个抽象类,旨在使用_read(size)
和_write(chunk, encoding, callback)
方法的底层实现进行扩展,就像使用Readable或Writable流类一样。
One notable exception is stream.PassThrough
, which is a simple echo stream implementation. 一个值得注意的例外是stream.PassThrough
,它是一个简单的回声流实现。
var PassThrough = require('stream').PassThrough;
var stream = new PassThrough;
Also note that ps
will be a global , making it directly accessible in all other modules. 另请注意, ps
将是全局的 ,使其可以在所有其他模块中直接访问。
If you simply want to use stream then you should do : 如果您只是想使用流,那么您应该:
var stream = new Stream;
stream.readable = stream.writable = true;
Duplex is meant for developers. Duplex适用于开发人员。 Some methods like _read
and _write
need to be implemented for it. 需要为它实现一些像_read
和_write
这样的方法。
[ Update ] [ 更新 ]
OK, you have data source, from the stdout. 好的,你有来自stdout的数据源。 You will need write
function, use this : 你需要write
函数,使用这个:
stream.write = function(data){this.emit('data', data);};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.