有沒有NodeJS'passthrough'流?是否有NodeJS'passthrough'流?
即一個對象,無論我放入它立即出來,不變。
這似乎毫無意義,但它在開發過程中作爲快速變化代碼的「靜態中心」是有用的。
有沒有NodeJS'passthrough'流?是否有NodeJS'passthrough'流?
即一個對象,無論我放入它立即出來,不變。
這似乎毫無意義,但它在開發過程中作爲快速變化代碼的「靜態中心」是有用的。
是的。其實,就是那個名字。 :)
它的問世與節點0.10後來由於Streams 2 update的一部分(在末尾提到)。
這也是從流的幾類可以直接實例之一:
var pass = new stream.PassThrough();
而且,它目前API for Stream Implementors下簡要記錄(向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);
出於好奇,如果它們必須共享相同的流引用,爲什麼將字節從一個TCP服務器傳遞到下一個服務器會有用? –
[也許](http://nodejs.org/api/stream.html#stream_class_stream_passthrough)(0.10+)。 –
@JonathanLonowski謝謝,我讀過那個頁面20次,仍然錯過了。讓它成爲答案,我會接受它。 – fadedbee