nodejs -- event 模块, 事件模块.

Posted cb_za

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nodejs -- event 模块, 事件模块.相关的知识,希望对你有一定的参考价值。

 

1. 注册事件 on 或者 addListener,触发事件 emit

 

1-1简单的使用:

 1 var EventEmitter =  require(\'events\').EventEmitter;
 2 
 3 var life = new EventEmitter();
 4 
 5 //求安慰:
 6 
 7 
 8 function water(who){
 9     console.log(\'给 \' +who+ \' 倒水\');
10 }
11 //具名函数
12 life.addListener(\'求安慰\', water);
13 
14 //匿名函数
15 life.addListener(\'求安慰\', function(who){
16     console.log(\'给 \' +who+ \' 揉肩\');
17 });
18 
19 
20 function cook(who){
21     console.log(\'给 \' +who+ \' 做饭\');
22 }
23 //具名函数
24 life.on(\'求安慰\', cook);
25 //匿名函数
26 life.on(\'求安慰\', function(who){
27     console.log(\'给 \' +who+ \' 洗衣服\');
28 });
29 
30 life.on(\'求安慰\', function(who){
31     console.log(\'给 \' +who+ \' .....5\');
32 });
33 
34 life.on(\'求安慰\', function(who){
35     console.log(\'给 \' +who+ \' .....6\');
36 });
37 
38 life.on(\'求安慰\', function(who){
39     console.log(\'给 \' +who+ \' .....7\');
40 });
41 
42 life.on(\'求安慰\', function(who){
43     console.log(\'给 \' +who+ \' .....8\');
44 });
45 
46 life.on(\'求安慰\', function(who){
47     console.log(\'给 \' +who+ \' .....9\');
48 });
49 
50 
51 //求溺爱
52 
53 life.on(\'求溺爱\', function(who){
54     console.log(\'给 \' +who+ \' 买包包1\');
55 });
56 
57 life.on(\'求溺爱\', function(who){
58     console.log(\'给 \' +who+ \' 看电影1\');
59 });
60 
61 life.on(\'求溺爱\', function(who){
62     console.log(\'给 \' +who+ \' 去旅游1\');
63 });
64 
65 //触发:
66 life.emit(\'求安慰\', \'汉子\');

 

 

  • 这里一共有两个事件, 求安慰,  一共有 9个动作;   求溺爱 ,一共有 3个动作
  • 添加事件监听 有 addListener()  和 on() 两种方法,  这两种方法都 可以 使用 具名函数 和 匿名函数.
  • emit() 触发事件,   上面 没有触发 [求溺爱]事件, 因此没有输出.

 

---------------------------------------

触发 [求溺爱] 事件.

1 //触发:
2 life.emit(\'求安慰\', \'汉子\');
3 
4 life.emit(\'求溺爱\', \'妹子\');

 

 

-----------------------------------------------------------------------

1-2emit(event, 参数1, 参数2,参数3) 传递多个参数的时候:

 1 var EventEmitter =  require(\'events\').EventEmitter;
 2 
 3 var life = new EventEmitter();
 4 
 5 
 6 
 7 //求安慰:
 8 
 9 
10 function water(who){
11     console.log(\'给 \' +who+ \' 倒水1\');
12 }
13 //具名函数
14 life.addListener(\'求安慰\', water);
15 
16 //匿名函数
17 life.addListener(\'求安慰\', function(who){
18     console.log(\'给 \' +who+ \' 揉肩2\');
19 });
20 
21 
22 function cook(who){
23     console.log(\'给 \' +who+ \' 做饭3\');
24 }
25 //具名函数
26 life.on(\'求安慰\', cook);
27 //匿名函数
28 life.on(\'求安慰\', function(who){
29     console.log(\'给 \' +who+ \' 洗衣服4\');
30 });
31 
32 life.on(\'求安慰\', function(){
33     console.log(\'这个是 第 5个 监听器\');
34 });
35 
36 life.on(\'求安慰\', function(address, age){
37     console.log(\'我要去\' +address+ \'旅游,我的年龄是\' +age );
38 });
39 
40 
41 
42 
43 
44 //触发:
45 life.emit(\'求安慰\', \'汉子\', \'北京\', 26);

红色部分显然是不符合我们的要求的.

---

解决办法: 使得 36行是 3个参数, 但是 第一个参数不使用 .

 

 1 var EventEmitter =  require(\'events\').EventEmitter;
 2 
 3 var life = new EventEmitter();
 4 
 5 
 6 
 7 //求安慰:
 8 
 9 
10 function water(who){
11     console.log(\'给 \' +who+ \' 倒水1\');
12 }
13 //具名函数
14 life.addListener(\'求安慰\', water);
15 
16 //匿名函数
17 life.addListener(\'求安慰\', function(who){
18     console.log(\'给 \' +who+ \' 揉肩2\');
19 });
20 
21 
22 function cook(who){
23     console.log(\'给 \' +who+ \' 做饭3\');
24 }
25 //具名函数
26 life.on(\'求安慰\', cook);
27 //匿名函数
28 life.on(\'求安慰\', function(who){
29     console.log(\'给 \' +who+ \' 洗衣服4\');
30 });
31 
32 life.on(\'求安慰\', function(){
33     console.log(\'这个是 第 5个 监听器\');
34 });
35 
36 life.on(\'求安慰\', function(who, address, age){
37     console.log(\'我要去\' +address+ \'旅游,我的年龄是\' +age );
38 });
39 
40 
41 
42 
43 
44 //触发:
45 life.emit(\'求安慰\', \'汉子\', \'北京\', 26);

 

 

 

 1-3:如何判断 事件有没有注册 监听器?

 

 

 1 var EventEmitter =  require(\'events\').EventEmitter;
 2 
 3 var life = new EventEmitter();
 4 
 5 
 6 
 7 //求安慰:
 8 
 9 
10 function water(who){
11     console.log(\'给 \' +who+ \' 倒水1\');
12 }
13 //具名函数
14 life.addListener(\'求安慰\', water);
15 
16 //匿名函数
17 life.addListener(\'求安慰\', function(who){
18     console.log(\'给 \' +who+ \' 揉肩2\');
19 });
20 
21 
22 function cook(who){
23     console.log(\'给 \' +who+ \' 做饭3\');
24 }
25 //具名函数
26 life.on(\'求安慰\', cook);
27 //匿名函数
28 life.on(\'求安慰\', function(who){
29     console.log(\'给 \' +who+ \' 洗衣服4\');
30 });
31 
32 life.on(\'求安慰\', function(){
33     console.log(\'这个是 第 5个 监听器\');
34 });
35 
36 life.on(\'求安慰\', function(who, address, age){
37     console.log(\'我要去\' +address+ \'旅游,我的年龄是\' +age );
38 });
39 
40 
41 
42 
43 
44 //触发:
45 var anwei = life.emit(\'求安慰\', \'汉子\', \'北京\', 26);
46 
47 var niai = life.emit(\'求溺爱\');
48 
49 console.log(anwei);
50 console.log(niai);

 

 可见 , [求安慰] 事件 注册了 监听器,   [求溺爱]事件 没有注册 监听器.

 

 

 

 

 

 

 

2. 设置事件 可以拥有的监听器数量.setMaxListeners(n)

   默认是10个:

 

 

 1 var EventEmitter =  require(\'events\').EventEmitter;
 2 
 3 var life = new EventEmitter();
 4 
 5 //求安慰:
 6 
 7 
 8 function water(who){
 9     console.log(\'给 \' +who+ \' 倒水\');
10 }
11 //具名函数
12 life.addListener(\'求安慰\', water);
13 
14 //匿名函数
15 life.addListener(\'求安慰\', function(who){
16     console.log(\'给 \' +who+ \' 揉肩\');
17 });
18 
19 
20 function cook(who){
21     console.log(\'给 \' +who+ \' 做饭\');
22 }
23 //具名函数
24 life.on(\'求安慰\', cook);
25 //匿名函数
26 life.on(\'求安慰\', function(who){
27     console.log(\'给 \' +who+ \' 洗衣服\');
28 });
29 
30 life.on(\'求安慰\', function(who){
31     console.log(\'给 \' +who+ \' .....5\');
32 });
33 
34 life.on(\'求安慰\', function(who){
35     console.log(\'给 \' +who+ \' .....6\');
36 });
37 
38 life.on(\'求安慰\', function(who){
39     console.log(\'给 \' +who+ \' .....7\');
40 });
41 
42 life.on(\'求安慰\', function(who){
43     console.log(\'给 \' +who+ \' .....8\');
44 });
45 
46 life.on(\'求安慰\', function(who){
47     console.log(\'给 \' +who+ \' .....9\');
48 });
49 
50 life.on(\'求安慰\', function(who){
51     console.log(\'给 \' +who+ \' .....10\');
52 });
53 
54 life.on(\'求安慰\', function(who){
55     console.log(\'给 \' +who+ \' .....11\');
56 });
57 
58 
59 //求溺爱
60 
61 life.on(\'求溺爱\', function(who){
62     console.log(\'给 \' +who+ \' 买包包1\');
63 });
64 
65 life.on(\'求溺爱\', function(who){
66     console.log(\'给 \' +who+ \' 看电影1\');
67 });
68 
69 life.on(\'求溺爱\', function(who){
70     console.log(\'给 \' +who+ \' 去旅游1\');
71 });
72 
73 //触发:
74 life.emit(\'求安慰\', \'汉子\');
75 
76 life.emit(\'求溺爱\', \'妹子\');

 

 

 

 

 --------------------------------------

增加 事件 的 最大监听器数量为 11.

 

  1 var EventEmitter =  require(\'events\').EventEmitter;
  2 
  3 var life = new EventEmitter();
  4 
  5 // 设置事件 允许  的 最多 监听器 的数量
  6 life.setMaxListeners(11);
  7 
  8 //求安慰:
  9 
 10 
 11 function water(who){
 12     console.log(\'给 \' +who+ \' 倒水\');
 13 }
 14 //具名函数
 15 life.addListener(\'求安慰\', water);
 16 
 17 //匿名函数
 18 life.addListener(\'求安慰\', function(who){
 19     console.log(\'给 \' +who+ \' 揉肩\');
 20 });
 21 
 22 
 23 function cook(who){
 24     console.log(\'给 \' +who+ \' 做饭\');
 25 }
 26 //具名函数
 27 life.on(\'求安慰\', cook);
 28 //匿名函数
 29 life.on(\'求安慰\', function(who){
 30     console.log(\'给 \' +who+ \' 洗衣服\');
 31 });
 32 
 33 life.on(\'求安慰\', function(who){
 34     console.log(\'给 \' +who+ \' .....5\');
 35 });
 36 
 37 life.on(\'求安慰\', function(who){
 38     console.log(\'给 \' +who+ \' .....6\');
 39 });
 40 
 41 life.on(\'求安慰\', function(who){
 42     console.log(\'给 \' +who+ \' .....7\');
 43 });
 44 
 45 life.on(\'求安慰\', function(who){
 46     console.log(\'给 \' +who+ \' .....8\');
 47 });
 48 
 49 life.on(\'求安慰\', function(who){
 50     console.log(\'给 \' +who+ \' .....9\');
 51 });
 52 
 53 life.on(\'求安慰\', function(who){
 54     console.log(\'给 \' +who+ \' .....10\');
 55 });
 56 
 57 life.on(\'求安慰\', function(who){
 58     console.log(\'给 \' +who+ \' .....11\');
 59 });
 60 
 61 
 62 //求溺爱
 63 
 64 life.on(\'求溺爱\', function(who){
 65     console.log(\'给 \' +who+ \' 买包包1\');
 66 });
 67 
 68 life.on(\'求溺爱\', function(who){
 69     console.log(\'给 \' +who+ \' 看电影2\');
 70 });
 71 
 72 life.on(\'求溺爱\', function(who){
 73     console.log(\'给 \' +who+ \' 去旅游3\');
 74 });
 75 
 76 life.on(\'求溺爱\', function(who){
 77     console.log(\'给 \' +who+ \' .....4\');
 78 });
 79 
 80 life.on(\'求溺爱\', function(who){
 81     console.log(\'给 \' +who+ \' .....5\');
 82 });
 83 
 84 life.on(\'求溺爱\', function(who){
 85     console.log(\'给 \' +who+ \' .....6\');
 86 });
 87 
 88 life.on(\'求溺爱\', function(who){
 89     console.log(\'给 \' +who+ \' .....7\');
 90 });
 91 
 92 life.on(\'求溺爱\', function(who){
 93     console.log(\'给 \' +who+ \' .....8\');
 94 });
 95 
 96 
 97 life.on(\'求溺爱\', function(who){
 98     console.log(\'给 \' +who+ \' .....9\');
 99 });
100 
101 
102 life.on(\'求溺爱\', function(who){
103     console.log(\'给 \' +who+ \' .....10\');
104 });
105 
106 
107 life.on(\'求溺爱\', function(who){
108     console.log(\'给 \' +who+ \' .....11\');
109 });
110 
111 
112 
113 
114 //触发:
115 life.emit(\'求安慰\', \'汉子\');
116 
117 life.emit(\'求溺爱\', \'妹子\');

 

 

  注意:

  1.   life.setMaxListeners(11); 在添加事件之前 设置 允许 的 事件 监听器 数量是 11个.
  2. 添加的事件监听器 数量 是 针对 所有的事件 生效的;   本 脚本内, 有两个事件, [求安慰] [求溺爱] 都 有了 11个 监听器.

 

 

 

3. 移除单个:  移除某个事件 的 某一个监听器. removeListener(event, listener)

 

 

  1 var EventEmitter =  require(\'events\').EventEmitter;
  2 
  3 var life = new EventEmitter();
  4 
  5 // 设置事件 允许  的 最多 监听器 的数量
  6 life.setMaxListeners(11);
  7 
  8 //求安慰:
  9 
 10 
 11 function water(who){
 12     console.log(\'给 \' +who+ \' 倒水\');
 13 }
 14 //具名函数
 15 life.addListener(\'求安慰\', water);
 16 
 17 //匿名函数
 18 life.addListener(\'求安慰\', function(who){
 19     console.log(\'给 \' +who+ \' 揉肩\');
 20 });
 21 
 22 
 23 function cook(who){
 24     console.log(\'给 \' +who+ \' 做饭\');
 25 }
 26 //具名函数
 27 life.on(\'求安慰\', cook);
 28 //匿名函数
 29 life.on(\'求安慰\', function(who){
 30     console.log(\'给 \' +who+ \' 洗衣服\');
 31 });
 32 
 33 life.on(\'求安慰\', function(who){
 34     console.log(\'给 \' +who+ \' .....5\');
 35 });
 36 
 37 life.on(\'求安慰\', function(who){
 38     console.log(\'给 \' +who+ \' .....6\');
 39 });
 40 
 41 life.on(\'求安慰\', function(who){
 42     console.log(\'给 \' +who+ \' .....7\');
 43 });
 44 
 45 life.on(\'求安慰\', function(who){
 46     console.log(\'给 \' +who+ \' .....8\');
 47 });
 48 
 49 life.on(\'求安慰\', function(who){
 50     console.log(\'给 \' +who+ \' .....9\');
 51 });
 52 
 53 life.on(\'求安慰\', function(who){
 54     console.log(\'给 \' +who+ \' .....10\');
 55 });
 56 
 57 life.on(\'求安慰\', function(who){
 58     console.log(\'给 \' +who+ \' .....11\');
 59 });
 60 
 61 
 62 //求溺爱
 63 
 64 life.on(\'求溺爱\', function(who){
 65     console.log(\'给 \' +who+ \' 买包包1\');
 66 });
 67 
 68 life.on(\'求溺爱\', function(who){
 69     console.log(\'给 \' +who+ \' 看电影2\');
 70 });
 71 
 72 life.on(\'求溺爱\', function(who){
 73     console.log(\'给 \' +who+ \' 去旅游3\');
 74 });
 75 
 76 life.on(\'求溺爱\', function(who){
 77     console.log(\'给 \' +who+ \' .....4\');
 78 });
 79 
 80 life.on(\'求溺爱\', function(who){
 81     console.log(\'给 \' +who+ \' .....5\');
 82 });
 83 
 84 life.on(\'求溺爱\', function(who){
 85     console.log(\'给 \' +who+ \' .....6\');
 86 });
 87 
 88 life.on(\'求溺爱\', function(who){
 89     console.log(\'给 \' +who+ \' .....7\');
 90 });
 91 
 92 life.on(\'求溺爱\', function(who){
 93     console.log(\'给 \' +who+ \' .....8\');
 94 });
 95 
 96 
 97 life.on(\'求溺爱\', function(who){
 98     console.log(\'给 \' +who+ \' .....9\');
 99 });
100 
101 
102 life.on(\'求溺爱\', function(who){
103     console.log(\'给 \' +who+ \' .....10\');
104 });
105 
106 
107 life.on(\'求溺爱\', function(who){
108     console.log(\'给 \' +who+ \' .....11\');
109 });
110 
111 
112 life.removeListener(\'求安慰\', water);
113 
114 //触发:
115 life.emit(\'求安慰\', \'汉子\');
116 
117 life.emit(\'求溺爱\', \'妹子\');

 

注意:

  1. 上面移除了 water 对应的 [倒水]事件.
  2. 这个函数只能移除 单个事件的 某个监听器
    1. 不可以移除 所有的事件 的 监听器
    2. 不可以移除 单个事件的 所有监听器,  removeListen(\'求安慰\') , 这种用法 会 报错
  3. 这个函数 只能 移除  具名函数 对应 的监听器 ;  匿名函数 注册的监听器 无法移除.

 

 

 

4. 判断 事件拥有的 监听器 的数量  listeners(event)    ,  listenerCount(emitter, event)

 以上是关于nodejs -- event 模块, 事件模块.的主要内容,如果未能解决你的问题,请参考以下文章

nodeJS之事件events

Node.js——NodeJs事件

NodeJS入门:常用模块汇总之事件模块

NodeJS入门:常用模块汇总之事件模块

Nodejs事件模块小实例

Backbone的event模块分析