Node.js數(shù)據(jù)流Stream之Duplex流和Transform流用法
Duplex流一個很好的例子是TCP套接字連接.需要實現(xiàn)_read(size)和_Write(data,encoding,callback)方法.
var stream = require('stream'); var util = require('util'); util.inherits(Duplexer, stream.Duplex); function Duplexer(opt) { stream.Duplex.call(this, opt); this.data = []; } Duplexer.prototype._read = function readItem(size) { var chunk = this.data.shift(); if (chunk == "stop"){ this.push(null); } else{ if(chunk){ this.push(chunk); } } }; Duplexer.prototype._write = function(data, encoding, callback) { this.data.push(data); callback(); }; var d = new Duplexer({allowHalfOpen:true}); d.on('data', function(chunk){ console.log('read: ', chunk.toString()); }); d.on('readable',function(){ console.log("readable"); }) d.on('end', function(){ console.log('Message Complete'); }); d.write("I think, "); d.write("therefore "); d.write("I am."); d.write("Rene Descartes"); d.write("stop");
輸出結(jié)果:
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_duplex.js
read: I think,
read: therefore
read: I am.
read: Rene Descartes
readable
readable
readable
Message CompleteProcess finished with exit code 0
Transform變換流擴展了Duplex流,不需要實現(xiàn)而是直接提供。但要實現(xiàn)_transform(chunk,encoding,callback)._flush()這個方法不知道用來做什么的目前
var stream = require("stream"); var util = require("util"); util.inherits(JSONObjectStream, stream.Transform); function JSONObjectStream (opt) { stream.Transform.call(this, opt); }; JSONObjectStream.prototype._transform = function (data, encoding, callback) { object = data ? JSON.parse(data.toString()) : ""; this.emit("object", object); object.handled = true; this.push(JSON.stringify(object)); callback(); }; var tc = new JSONObjectStream(); tc.on("object", function(object){ console.log("Name: %s", object.name); console.log("Color: %s", object.color); }); tc.on("data", function(data){ console.log("Data: %s", data.toString()); }); tc.write('{"name":"Carolinus", "color": "Green"}'); tc.write('{"name":"Solarius", "color": "Blue"}'); tc.write('{"name":"Lo Tae Zhao", "color": "Gold"}'); tc.write('{"name":"Ommadon", "color": "Red"}');
輸出結(jié)果:
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_transform.js
Name: Carolinus
Color: Green
Data: {"name":"Carolinus","color":"Green","handled":true}
Name: Solarius
Color: Blue
Data: {"name":"Solarius","color":"Blue","handled":true}
Name: Lo Tae Zhao
Color: Gold
Data: {"name":"Lo Tae Zhao","color":"Gold","handled":true}
Name: Ommadon
Color: Red
Data: {"name":"Ommadon","color":"Red","handled":true}Process finished with exit code 0
到此這篇關(guān)于Node.js數(shù)據(jù)流Stream之Duplex流和Transform流的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Node中使用http-proxy-middleware實現(xiàn)代理跨域的方法步驟
本文主要介紹了Node中使用http-proxy-middleware實現(xiàn)代理跨域的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11win7下安裝配置node.js+express開發(fā)環(huán)境
windows7下安裝nodejs及框架express,從誕生至今一直被熱捧,筆者最近也裝了個環(huán)境打算了解一下。安裝步驟簡單比較簡單,這里分享給大家,希望大家能夠喜歡。2015-12-12Node.js中Path 模塊的介紹和使用示例小結(jié)
Node.js path 模塊提供了一些用于處理文件路徑的小工具,下面通過本文給大家介紹Node.js中Path 模塊的介紹和使用示例小結(jié),感興趣的朋友跟隨小編一起看看吧2024-05-05