에서 이벤트 발광 내가 이벤트를 방출하는 편집하기 위해 노력하고있어 소켓을 사용하는 라이브러리를 가지고 광범위하게 코드는 다음과 같습니다Node.js를
function Library(port, host) {
this.myData = [];
this.socket = net.connect(...);
this.socket.on('data', this._ondata.bind(this));
this.socket.on('error', this._onerror.bind(this));
this.socket.on('close', this._onclose.bind(this));
}
Library.prototype._ondata = function() {
//do stuff
//have the data we want
this.myData.push(stuff);
this.socket.end();
}
Library.protoype._onclose = function() {
console.log('this gets logged');
}
modules.export = Library;
내가에서 이벤트를 방출 할 난 그냥 바로 lib.myData
에 액세스하는 경우가 여전히 빈 상태 (empty)의 배열이기 때문에이
var lib = new Library(port, host);
lib.on('emitted-event', function() {
var data = lib.myData;
});
처럼 뭔가를 할 수 있도록 _onclose
방법. 하지만 나는 사건을 일으키기 위해 고심하고있다. 내가 지금 얻고있는 것은 도서관에 방법이 없다는 것이다. 그래서 명확하게 나는 에미 터를 올바른 방법으로 추가하지 않을 것입니다. 누구가 그게 뭔지 알아?
http://www.sitepoint.com/nodejs-events-and-eventemitter/ – bryanmac
'this.MyData.push (물건)''해야 this.myData.push (물건); ' – SergioFilhow