nodejs Duplex流要求实现者同时指定write和read方法:
import stream from 'stream';
const duplex = new stream.Duplex({
write: (chunk,encoding,next) {
// Do something with the chunk and then call next() to indicate
// that the chunk has been processed. The write() fn will handle
// data piped into this duplex stream. After the write() has
// finished,the data will be processed by the read() below.
next();
},read: ( size ) {
// Add new data to be read by streams piped from this duplex
this.push( "some data" )
}
})
有关流的官方nodejs文档可在这里找到:API for Stream Implementers
网络套接字方案
上述的websocket示例可能应该使用Readable而不是双工流。双工流在存储转发或处理转发方案中很有用。但是,听起来好像websocket示例中的流仅用于将数据从websocket移动到流接口。这可以使用Readable来实现:
import stream from 'stream';
const onSocketConnection = ( socket ) => {
const readable = new stream.Readable({
// The read logic is omitted since the data is pushed to the socket
// outside of the script's control. However,the read() function
// must be defined.
read(){}
});
socket.on('message',( data ) => {
// Push the data on the readable queue
readable.push( data );
});
readable.pipe( ffmpeg );
}
本文链接:https://www.f2er.com/3145240.html