EventEmitter class
In the event module of Node.js, which is used to implement various event processing, an EventEmitter class is defined. All objects that may trigger events are instance objects that integrate a subclass of the EventEmitter class. In Node.js, many methods are defined for the EventEmitter class. All processing related to the binding and unbinding of the object's event processing function is Execution relies on calls to these methods.
event: represents the event name
listener: represents the event processing function
The parameters in square brackets represent that the parameters are optional parameters
on method of EventEmitter class
##
var http = require("http"); var server = http.createServer(); server.on("request", function(req, res){ console.log(req.url); res.end(); }); server.listen(1337, "127.0.0.1");
Console output
var http = require("http"); var server = http.createServer(); server.on('request', function(req, res){ console.log('接收到客户端请求') }) server.on("request", function(req, res){ console.log('处理客户端请求') console.log(req.url); res.end(); }) server.on('request', function(req, res){ console.log('发送响应完毕') }) server.listen(1337, "127.0.0.1");
Console output
emitter.setMaxListeners(n)
once method of the EventEmitter class
emitter.once(event, listener)
var http = require("http"); var server = http.createServer(); server.on('request', function(req, res){ console.log('接收到客户端请求') }) server.on("request", function(req, res){ console.log('处理客户端请求') console.log(req.url); res.end(); }) server.on('request', function(req, res){ console.log('发送响应完毕') }) server.listen(1337, "127.0.0.1");
is displayed twice
var http = require("http"); var server = http.createServer(); server.once('request', function(req, res){ console.log('接收到客户端请求') }) server.on("request", function(req, res){ console.log('处理客户端请求') console.log(req.url); res.end(); }) server.once('request', function(req, res){ console.log('发送响应完毕') }) server.listen(1337, "127.0.0.1");
The request is processed 2 times, and the rest are only printed once!
Use the removeListener method to cancel the event processing function
var http = require("http"); var server = http.createServer(); var testFunction = function (req,res) { console.log('发送响应完毕') } server.on('request', function(req, res){ console.log('接收到客户端请求') }) server.on("request", function(req, res){ console.log('处理客户端请求') console.log(req.url); res.end(); }) server.on('request', testFunction) //删除 server.removeListener('request', testFunction) server.listen(1337, "127.0.0.1");
Console output
emit method: Custom event and trigger it
var http = require("http"); var server = http.createServer(); server.on("request", function(req, res){ console.log(req.url); }); //自定义事件 server.on("customEvent", function(arg1, arg2, arg3){ console.log("自定义事件被触发"); console.log(arg1); console.log(arg2); console.log(arg3); }); //触发自定义事件 server.emit('customEvent', '自定义参数1', '自定义参数2', '自定义参数3') server.listen(1337, "127.0.0.1");
Console output
The above is the detailed content of Detailed explanation of various usage codes of EventEmitter class in node.js. For more information, please follow other related articles on the PHP Chinese website!