是否有 NodeJS 的“直通”流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19445217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a NodeJS 'passthrough' stream?
提问by fadedbee
Is there a NodeJS 'passthrough' stream?
是否有 NodeJS 的“直通”流?
i.e. an object where whatever I put in to it comes out immediately, unchanged.
即一个对象,无论我放入它什么,它都会立即出现,不变。
It seems pointless, but it would be useful as a 'static centre' for rapidly changing code during development.
这似乎毫无意义,但它作为开发过程中快速更改代码的“静态中心”会很有用。
回答by Jonathan Lonowski
Yeah. Actually, by that very name. :)
是的。事实上,就这个名字。:)
It's available with Node 0.10 and later as part of the Streams 2 update(mentioned at the end).
它可用于 Node 0.10 及更高版本,作为Streams 2 更新的一部分(在最后提到)。
It's also one of the few types from Streams that can be directly instantiated:
它也是 Streams 中可以直接实例化的少数类型之一:
var pass = new stream.PassThrough();
And, it's currently documented briefly under API for Stream Implementors(towards the bottom of the Steams ToC).
而且,它目前在流实现者的 API下有简要记录(朝向Steams ToC的底部)。
回答by sukanta
it's really handy when you need to send input bytes of a TCP Server to another TCP Server.
当您需要将 TCP 服务器的输入字节发送到另一个 TCP 服务器时,它真的很方便。
In my microntoller application's web part i am using this as follows
在我的 microntoller 应用程序的 web 部件中,我使用它如下
var net = require('net'),
PassThroughStream = require('stream').PassThrough,
stream = new PassThroughStream();
net.createServer({allowHalfOpen: true}, function(socket) {
socket.write("Hello client!");
console.log('Connected:' + socket.remoteAddress + ':' + socket.remotePort);
socket.pipe(stream, {end: false});
}).listen(8080);
net.createServer(function(socket) {
stream.on('data', function (d) {
d+='';
socket.write(Date() + ':' + ' ' + d.toUpperCase());
});
socket.pipe(stream);
}).listen(8081);

