- JavaScript:Moving to ES2015
- Ved Antani Simon Timms Narayan Prusty
- 401字
- 2021-07-09 19:07:44
EventEmitters
We discussed earlier that callbacks are great for the execution of one-off logic. EventEmitters are useful in responding to repeating events. EventEmitters fire events and include the ability to handle these events when triggered. Several important Node APIs are built on EventEmitters.
Events raised by EventEmitters are handled through listeners. A listener is a callback function associated with an event—when the event fires, its associated listener is triggered as well. The event.EventEmitter
is a class that is used to provide a consistent interface to emit (trigger) and bind callbacks to events.
As a common style convention, event names are represented by a camel-cased string; however, any valid string can be used as an event name.
Use require('events')
to access the EventEmitter
class:
var EventEmitter = require('events');
When an EventEmitter instance encounters an error, it emits an error
event. Error events are treated as a special case in Node.js. If you don't handle these, the program exits with an exception stack.
All EventEmitters emit the newListener
event when new listeners are added and removeListener
when a listener is removed.
To understand the usage of EventEmitters, we will build a simplistic telnet server where different clients can log in and enter certain commands. Based on these commands, our server will respond accordingly:
var _net = require('net'); var _events = require ('events'); var _emitter = new events.EventEmitter(); _emitter.on('join', function(id,caller){ console.log(id+" - joined"); }); _emitter.on('quit', function(id,caller){ console.log(id+" - left"); }); var _server = _net.createServer(function(caller) { var process_id = caller.remoteAddress + ':' + caller.remotePort; _emitter.emit('join',id,caller); caller.on('end', function() { console.log("disconnected"); _emitter.emit('quit',id,caller); }); }); _server.listen(8124);
In this code snippet, we are using the net
module from Node. The idea here is to create a server and let the client connect to it via a standard telnet
command. When a client connects, the server displays the client address and port, and when the client quits, the server logs this too.
When a client connects, we are emitting a join
event, and when the client disconnects, we are emitting a quit
event. We have listeners for both these events and they log appropriate messages on the server.
You start this program and connect to our server using telnet as follows:
telnet 127.0.0.1 8124
On the server console, you will see the server logging which client joined the server:
? node app.js ::ffff:127.0.0.1:51000 - joined ::ffff:127.0.0.1:51001 – joined
If any client quits the session, an appropriate message will appear as well.
- 精通Python自然語(yǔ)言處理
- Keras深度學(xué)習(xí)實(shí)戰(zhàn)
- Building Android UIs with Custom Views
- 軟件項(xiàng)目管理實(shí)用教程
- Kotlin極簡(jiǎn)教程
- Natural Language Processing with Python Quick Start Guide
- Oracle實(shí)用教程
- Building Slack Bots
- Python Web自動(dòng)化測(cè)試設(shè)計(jì)與實(shí)現(xiàn)
- Docker:容器與容器云(第2版)
- PHP項(xiàng)目開(kāi)發(fā)全程實(shí)錄(第4版)
- Mastering Bootstrap 4
- 啊哈C語(yǔ)言!:邏輯的挑戰(zhàn)(修訂版)
- Design Patterns and Best Practices in Java
- Hands-On ROS for Robotics Programming