nodejs EventEmitter
来源:互联网 发布:排课软件km1199 编辑:程序博客网 时间:2024/05/19 13:19
Many objects in Node emit events: a net.Server
emits an event each timea peer connects to it, afs.readStream
emits an event when the file isopened. All objects which emit events are instances ofevents.EventEmitter
.You can access this module by doing: require("events");
Typically, event names are represented by a camel-cased string, however,there aren't any strict restrictions on that, as any string will be accepted.
Functions can then be attached to objects, to be executed when an eventis emitted. These functions are calledlisteners. Inside a listenerfunction, this
refers to the EventEmitter
that the listener wasattached to.
Class: events.EventEmitter#
To access the EventEmitter class, require('events').EventEmitter
.
When an EventEmitter
instance experiences an error, the typical action isto emit an'error'
event. Error events are treated as a special case in node.If there is no listener for it, then the default action is to print a stacktrace and exit the program.
All EventEmitters emit the event 'newListener'
when new listeners areadded and'removeListener'
when a listener is removed.
emitter.addListener(event, listener)#
emitter.on(event, listener)#
Adds a listener to the end of the listeners array for the specified event
.No checks are made to see if thelistener
has already been added. Multiplecalls passing the same combination ofevent
and listener
will result in thelistener
being added multiple times.
server.on('connection', function (stream) { console.log('someone connected!');});
Returns emitter, so calls can be chained.
emitter.emit(event, [arg1], [arg2], [...])#
Execute each of the listeners in order with the supplied arguments.
Returns true
if event had listeners, false
otherwise.
例子:
var EventEmitter = require('events').EventEmitter;var event = new EventEmitter();event.on('some_event', function(who, opcode, opobject){ console.log("listener 1 response to some_event"); console.log("%s %s %s\n", who, opcode, opobject); });event.on('some_event', function(){ console.log("listener 2 response to some_event");});setTimeout(function(){ event.emit('some_event', "xiaoming", "eat", "rice");}, 1000);
结果输出:
listener 1 response to some_eventxiaoming eat ricelistener 2 response to some_event
- nodejs EventEmitter
- nodejs-EventEmitter
- nodejs的事件EventEmitter
- nodejs eventEmitter源码解读
- nodejs之EventEmitter
- NodeJs--事件驱动(EventEmitter)
- Nodejs学习 2 -- events事件处理EventEmitter
- nodejs为其他模块添加EventEmitter
- NodeJS学习系列课程笔记(NodeJs EventEmitter相关使用方法)
- NodeJS:通过EventEmitter为对象注册和触发事件
- Nodejs学习item 2 -- events事件处理EventEmitter
- nodejs系列学习:事件发射器EventEmitter与android中的eventBus对比-----(四)
- EventEmitter 使用
- node eventEmitter
- EventEmitter(事件监听/发射器 )
- Node.js EventEmitter
- javascript简单实现 EventEmitter
- Node.js EventEmitter类
- 红米2 移动版HM2014813专用中文TWRP2.8.4.1 recovery
- 内网与外网间通信
- C语言之二维数组
- linux学习笔记 (十五)开启端口
- TWaver3D特效系列之环境映射
- nodejs EventEmitter
- not(expr|ele|fn) 删除与指定表达式匹配的元素
- tomcat,apache启动文件
- 测试人员会像恐龙一样从地球上消失吗?
- 如何用未越狱手机安装ipa文件
- zoj 1016 Parencodings
- 【012】命名空间和模块化编程
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
- jdk中自带的转码工具native2ascii