Node.js数据流Stream之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.js
read: I think,
read: therefore
read: I am.
read: Rene Descartes
readable
readable
readable
Message Complete Process 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.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
Node.js数据流Stream之Duplex流和Transform流的更多相关文章
- Node.js数据流Stream之Readable流和Writable流
一.前传 Stream在很多语言都会有,当然Node.js也不例外.数据流是可读.可写.或即可读又可写的内存结构.Node.js中主要包括Readable.Writable.Duplex(双工)和Tr ...
- 【Node.js】Stream(流)的学习笔记
最近学习使用Node.js创建http proxy server,少不了要跟Stream打交道.昨天开始查阅一些资料,多少有了一些粗浅了解.整理在这里,供学习之用. 从Node.js API文档中可知 ...
- Node.js:Stream(流)
Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请求的request 对象就是一个 Stream,还有stdout(标准输出). Node.js,Str ...
- Node.js 【Stream之笔记】
从Node.js API文档中可知, 'A stream is an abstract interface implemented by various objects in Node. For ex ...
- node.js中stream流中可读流和可写流的使用
node.js中的流 stream 是处理流式数据的抽象接口.node.js 提供了很多流对象,像http中的request和response,和 process.stdout 都是流的实例. 流可以 ...
- 理解 Node.js 中 Stream(流)
Stream(流) 是 Node.js 中处理流式数据的抽象接口. stream 模块用于构建实现了流接口的对象. Node.js 提供了多种流对象. 例如,对 HTTP 服务器的request请求和 ...
- Node.js 使用Stream的pipe(管道)方法实现文件复制
Stream模块有一个pipe方法,可以将两个流串起来,实现所有的数据自动从Readable流进入Writable流 "use strict"; const fs = requir ...
- 【node.js】Stream(流)
Stream 有四种流类型: Readable - 可读操作. Writable - 可写操作. Duplex - 可读可写操作. Transform - 操作被写入数据,然后读出结果. 所有的 St ...
- Node.js 内置模块Stream(流)
"流"是一种抽象的数据结构 通过使用"流"可以将一段数据分割成几段,并按顺序传输,使用"流"可以降低对系统性能的要求,减少对CPU的消耗 S ...
随机推荐
- 在Windows安装Reids 详解
今天安装了redis,记录点经验 因为Redis项目没有正式支持Windows. 但Microsoft开发和维护一个针对Windows 64版的redis. 下载地址在微软的GitHub上,地址:ht ...
- Day5 作业(完成)
1,有如下变量(tu是个元祖),请实现要求的功能# tu = ("alex", [11, 22, {"k1": 'v1', "k2": [& ...
- 741. Cherry Pickup
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- Chrome浏览器插件开发-淘宝自动登录
浏览器插件的介绍 Chrome浏览器插件开发的准备工作 manifest.json配置介绍 页面如何注入scripts文件 一. 浏览器插件的介绍 浏览器插件是一种遵循一定规范的应用程序接口编写出来的 ...
- uiautomator2
uiautomator2 该项目正在火热的开发中 uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库.其底层基于Google uiautomator,Go ...
- jzoj5906
我們首先發現,每一條邊都至少走1次,因為我們必須走到每一個節點按按鈕 如果我們不走一個節點,說明這個節點已經有傳送門了,但是必須走到這個節點開傳送門,矛盾 然後我們發現,每一條邊至多經過2次 如果我們 ...
- Map-560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- Topological Sor-207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Dynamic Programming-650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- apache测试网页执行效率
apache软件下有一个测试网页访问速度的工具ab.exe,位于apache的bin目录下,windows下使用命令行进入bin目录,执行ab.exe -n 10000 -c 10 http://12 ...