是否有NodeJS'passthrough'流?
即一个物体,无论我放入它的是什么,都会立即出现,不变.
这看起来毫无意义,但它可以作为开发期间快速更改代码的"静态中心".
Jon*_*ski 38
是啊.实际上,就是那个名字.:)
它可以与Node 0.10及更高版本一起使用,作为Streams 2更新的一部分(最后提到).
它也是Streams中可以直接实例化的少数类型之一:
var pass = new stream.PassThrough();
Run Code Online (Sandbox Code Playgroud)
而且,它目前在流实施者的API下简要记录(在Steams ToC的底部).
当您需要将 TCP 服务器的输入字节发送到另一个 TCP 服务器时,它真的很方便。
在我的 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19605 次 |
| 最近记录: |