node.js中的emitter.on方法使用说明_node.js
程序员文章站
2022-05-27 20:40:31
...
方法说明:
emitter.on(event, listener)
emitter.addListener(event, listener)
server.on('connection', function (stream) {
console.log('someone connected!');
});
EventEmitter.prototype.addListener = function(type, listener) {
var m;
if (!util.isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events)
this._events = {};
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (this._events.newListener)
this.emit('newListener', type,
util.isFunction(listener.listener) ?
listener.listener : listener);
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
else if (util.isObject(this._events[type]))
// If we've already got an array, just append.
this._events[type].push(listener);
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
// Check for listener leak
if (util.isObject(this._events[type]) && !this._events[type].warned) {
var m;
if (!util.isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
console.trace();
}
}
return this;
};
为指定事件注册一个监听器。
语法:
复制代码 代码如下:
emitter.on(event, listener)
emitter.addListener(event, listener)
接收参数:
event (string) 事件类型
listener (function) 触发事件时的回调函数
例子:
复制代码 代码如下:
server.on('connection', function (stream) {
console.log('someone connected!');
});
源码:
复制代码 代码如下:
EventEmitter.prototype.addListener = function(type, listener) {
var m;
if (!util.isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events)
this._events = {};
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (this._events.newListener)
this.emit('newListener', type,
util.isFunction(listener.listener) ?
listener.listener : listener);
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
else if (util.isObject(this._events[type]))
// If we've already got an array, just append.
this._events[type].push(listener);
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
// Check for listener leak
if (util.isObject(this._events[type]) && !this._events[type].warned) {
var m;
if (!util.isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
console.trace();
}
}
return this;
};
推荐阅读
-
一个简单的node.js界面实现方法
-
node.js中实现kindEditor图片上传功能的方法教程
-
推荐分享Node.js中18个值得了解的Web框架和工具
-
浅析Node.js 中 Stream API 的使用
-
Node.js fs模块(文件模块)创建、删除目录(文件)读取写入文件流的方法
-
Node.js Koa2使用JWT进行鉴权的方法示例
-
node.JS的crypto加密模块使用方法详解(MD5,AES,Hmac,Diffie-Hellman加密)
-
了不起的node.js读书笔记之node.js中的特性_node.js
-
node.js读取文件到字符串的方法_node.js
-
Node.js中.pfx后缀文件的处理方法