这篇文章主要介绍“node.js的Duplex流和TransfORM流怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“node.js的Duplex流和Transform流怎么使用”文章能帮助大
这篇文章主要介绍“node.js的Duplex流和TransfORM流怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“node.js的Duplex流和Transform流怎么使用”文章能帮助大家解决问题。
Duplex流一个很好的例子是tcp套接字连接.需要实现_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");
输出结果:
"C:\Program Files (x86)\JetBrains\WEBStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_duplex.jsread: I think, read: therefore read: I am.read: Rene DescartesreadablereadablereadableMessage CompleteProcess finished with exit code 0
Transform变换流扩展了Duplex流,不需要实现而是直接提供。但要实现_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"}');
输出结果:
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_transform.jsName: CarolinusColor: GreenData: {"name":"Carolinus","color":"Green","handled":true}Name: SolariusColor: BlueData: {"name":"Solarius","color":"Blue","handled":true}Name: Lo Tae ZhaoColor: GoldData: {"name":"Lo Tae Zhao","color":"Gold","handled":true}Name: OmmadonColor: RedData: {"name":"Ommadon","color":"Red","handled":true}Process finished with exit code 0
关于“Node.js的Duplex流和Transform流怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。
--结束END--
本文标题: Node.js的Duplex流和Transform流怎么使用
本文链接: https://www.lsjlt.com/news/342811.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0