Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

流(Stream) #5

Open
coxo opened this issue Dec 24, 2015 · 3 comments
Open

流(Stream) #5

coxo opened this issue Dec 24, 2015 · 3 comments

Comments

@coxo
Copy link
Member

coxo commented Dec 24, 2015

流(Stream)是可读,可写或双工的。

可以通过require('stream')加载流的基类,其中包括四类流,

  • Readable 流
  • Writable 流
  • Duplex 流
  • Transform流的基类

另外如果觉得上述四类基类流不能满足需求,可以编写自己的扩充类流。
像我的BearStream。

@coxo
Copy link
Member Author

coxo commented Dec 24, 2015

SimpleProtocol.prototype = Object.create(
 //SimpleProtocol继承readable类
  Readable.prototype, { constructor: { value: SimpleProtocol }}
);

@daxiongjun
Copy link
Member

补充个板栗

自定义readable stream的实现

var Stream = require('stream');
var Read = Stream.Readable;
var util = require('util');

util.inherits(MyReadStream, Read);

function MyReadStream(data, opt) {
    Read.call(this, opt);
    this.data = data || [];
}
MyReadStream.prototype._read = function () {
    var _this = this;
    this.data.forEach(function (d) {
        _this.push(d);
    })
    this.push(null);
}

var data = ['aa', 'bb', 'cc'];
var r = new MyReadStream(data);

r.on('data', function (chunk) {
    console.log(chunk.toString());
})

@VaJoy
Copy link
Member

VaJoy commented Dec 25, 2015

@daxiongjun 大熊去学markdown怎么用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants