Nodejs流模块

							Node.js Stream模块
一、	Readable流

事件:
1、	readable:在数据块可以从流中读取的时候发出。
2、	data:类似于readable,不同之处:当数据的事件处理程序被连接时,流被转变成流动的模式,且数据块处理程序被连续的调用,直至所有数据被调用完。
3、	end:当数据将不再被提供时由流发出
4、	close:关闭时
5、	error:接受数据中出现错误时发出

方法:
1、	read([size]):从流中读取数据
2、	setEncoding(encoding):设置read()读取返回时使用的编码
3、	pause():暂停从该对象发出的data事件
4、	resume():恢复从该对象发出的data事件

自定义Readable流对象:
1、	继承Readable流的功能。使用util模块的inherits()方法
2、	创建对象调用的实例。
3、	调用push()来输出Readable对象中的数据的_read()方法。



二、	Writable流

事件:
1、	drain:在write()调用返回false后,当准备好开始写更多的数据时发出此事件
2、	finish:当end()在Writable对象上被调用,所有的数据被刷新,且不会有更多的数据将被接受时发出此事件
3、	pipe:当pipe()方法在Readable流上被调用,以添加此Writable为目的地时,发出此事件.
4、	unpipe: 当unpipe()方法在Readable流上被调用,以删除此Writable为目的地时,发出此事件.

方法:
1、	write(chunk,[encoding],[callback]):将数据块写入流对象的数据位置。
2、	end(chunk,[encoding],[callback]):与write()相同,除了:end把Writable对象置于不在接受数据状态,并发送finish事件。

自定义Writable流对象:
1、	继承Readable流的功能。使用util模块的inherits()方法
2、	创建对象调用的实例。
3、调用push()来输出Writable对象中的数据的_write(chunk,[encoding],[callback])。




三、	Duplex流(双向流)
Duplex是结合可读可写功能的流

自定义Writable流对象:
1、	继承Readable流的功能。使用util模块的inherits()方法
2、	创建对象调用的实例。
3、同时实现Duplex对象中的数据的_read()和_write(chunk,encoding,callback)。




四、	Transform流

1、Transform流扩展了Duplex流,但他修改了Writable流和Readable流之间的数据。
2、Transform流不需要实现_read()和_write()原型方法。
3、要实现_transform(chunk,encoding,callback)和_flush(callback)方法。

Readable流实例:
var stream = require("stream");
var util = require("util");
util.inherits(MyReadable,stream.Readable);
function MyReadable(opt)
{
	stream.Readable.call(this,opt);
	this.str = "abcdefg";
	this._index = 0;
}
MyReadable.prototype._read = function()
{
	
	if(this._index>this.str.length)
	{
		this.push(null);
	}
	else
	{
		this.push(this.str.charAt(this._index));
		this._index += 1;
	}
	 
} 
  
var r = new MyReadable();
console.log("1111"+r.read());
 r.on("data",function(data)
 {
 	console.log("OK:"+data.toString());
 	//暂停流
 	this.pause();
 	resu();
 });
 function resu() {
 	 console.log("调用resume后。。。。。");
 	 //恢复流
 	 r.resume();
 }
r.on("end",function(data)
{
	console.log("the ending......");
});
 
<img src="https://img-blog.csdn.net/20160918223416187?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



Transform流实例:
var http = require("http");
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()):"";  //转化为JavaScript对象
	this.emit("object",object);   //触发事件
	object.handled = true;   
	this.push(JSON.stringify(object));//转化为字符串
	callback();
}
JSONObjectStream.prototype._flush = function(callback)
{
	callback();
}
 
http.createServer(function(request,response){
	response.writeHead(200,{"Content-Type":"text/html;charset = utf-8"});
	if(request.url !== "/favicon.ico")
	{
		 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":"zhangsan","color":"black"}');
		tc.write('{"name":"lisi","color":"blue"}');
		tc.write('{"name":"wangwu","color":"red"}');
		tc.write('{"name":"zhaoliu","color":"white"}');
		tc.write('{"name":"liuda","color":"green"}');


		response.end();
	}
}).listen(8000);
console.log("start running......");
 
<img src="https://img-blog.csdn.net/20160918223425187?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值