Home >Web Front-end >JS Tutorial >Detailed explanation of various usage codes of EventEmitter class in node.js

Detailed explanation of various usage codes of EventEmitter class in node.js

伊谢尔伦
伊谢尔伦Original
2017-07-24 10:53:351517browse

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");

In this code, we specify that when the server receives the client When requesting, output the URL address of the target requested by the client in your console window, and use the end method of the response object to immediately end the response.

Execute the code, and then enter: http://localhost:1337:// in the browser window. The console output is as follows:

Console output

Of course, you can also bind multiple event processing functions to the same event through the execution of multiple on methods. As follows:


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");

OK, execute the code, the console output is as follows:

Console output

In addition, by default, up to 10 time processing functions can be bound to the same specified event. You can modify the maximum number of event processing functions that can be bound through the setMaxListeners method as follows:


emitter.setMaxListeners(n)

once method of the EventEmitter class

The once method of the EventEmiiter class is similar to the on method. Their function is to bind the event processing function to the specified event. The difference is that when the event processing function is executed once, it will be contacted immediately, that is, the event processing function will only be executed once. . The parameters used in the once method are the same as those used in the on method, as follows:


emitter.once(event, listener)

Do an experiment.

Still execute the following code (same as above):


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");

Then, open 127.0.0.1:1337 twice in a row in the browser window, and the console outputs As follows:

is displayed twice

Then change the on event to once event, the code is as follows:


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 console output is as follows:

The request is processed 2 times, and the rest are only printed once!

Use the removeListener method to cancel the event processing function

The code is as follows:


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");

Run the code, enter 127.0.0.1:1337 in the browser window, the console output is as follows

Console output

emit method: Custom event and trigger it

The code is as follows:


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");

This time do not enter the address in the browser window, run the code directly to view Console output, the console output is as follows:

Console output

indicates that we manually triggered the custom event, which is customEvent.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn