简体   繁体   English

是否有 NodeJS 的“直通”流?

[英]Is there a NodeJS 'passthrough' stream?

Is there a NodeJS 'passthrough' stream?是否有 NodeJS 的“直通”流?

ie 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.这似乎毫无意义,但它可以作为一个“静态中心”,在开发过程中快速更改代码。

Yeah.是的。 Actually, by that very name.其实,就是这个名字。 :) :)

stream.PassThrough

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 for Stream Implementors下(靠近Steams ToC的底部)。

it's really handy when you need to send input bytes of a TCP Server to another TCP Server.当您需要将 TCP Server 的输入字节发送到另一个 TCP Server 时,它真的很方便。

In my microcontroller application's web part i am using this as follows在我的微控制器应用程序的 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM