Node.js數(shù)據(jù)流Stream之Duplex流和Transform流用法
Duplex流一個(gè)很好的例子是TCP套接字連接.需要實(shí)現(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變換流擴(kuò)展了Duplex流,不需要實(shí)現(xiàn)而是直接提供。但要實(shí)現(xiàn)_transform(chunk,encoding,callback)._flush()這個(gè)方法不知道用來做什么的目前
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流的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Node.js中的流(Stream)的作用詳解
- node.js同步/異步文件讀寫-fs,Stream文件流操作實(shí)例詳解
- Node.js數(shù)據(jù)流Stream之Readable流和Writable流用法
- node.js中stream流中可讀流和可寫流的實(shí)現(xiàn)與使用方法實(shí)例分析
- node.js使用stream模塊實(shí)現(xiàn)自定義流示例
- Node.js中你不可不精的Stream(流)
- Node.js中流(stream)的使用方法示例
- Node.js中的流(Stream)介紹
- Node.js 中的流Stream模塊簡(jiǎn)介及如何使用流進(jìn)行數(shù)據(jù)處理
相關(guān)文章
node使用querystring內(nèi)置模塊解決分頁返回?cái)?shù)據(jù)太多導(dǎo)致json.parse()解析報(bào)錯(cuò)問題
這篇文章主要介紹了node使用querystring內(nèi)置模塊解決分頁返回?cái)?shù)據(jù)太多導(dǎo)致json.parse()解析報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09
Nest.js系列學(xué)習(xí)之初識(shí)nest項(xiàng)目框架及服務(wù)
這篇文章主要為大家介紹了Nest.js系列學(xué)習(xí)之初識(shí)nest項(xiàng)目框架及服務(wù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
nodejs基于express實(shí)現(xiàn)文件上傳的方法
這篇文章主要介紹了nodejs基于express實(shí)現(xiàn)文件上傳的方法,結(jié)合實(shí)例形式分析了nodejs基于express框架實(shí)現(xiàn)文件上傳功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
nodejs對(duì)express中next函數(shù)的一些理解
這篇文章主要介紹了nodejs對(duì)express中next函數(shù)的一些理解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
nodejs express搭建服務(wù)并熱更新文件過程詳解
這篇文章主要為大家介紹了nodejs express搭建服務(wù)并熱更新文件過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

