收听Node.js中的所有发布的事件 [英] Listen to All Emitted Events in Node.js
问题描述
在Node.js中有什么方法来收听EventEmitter对象发出的所有事件?
In Node.js is there any way to listen to all events emitted by an EventEmitter object?
例如,你能做些什么吗喜欢...
e.g., can you do something like...
event_emitter.on('',function(event[, arg1][, arg2]...) {}
这个想法是我想抓住服务器端发出的所有事件 EventEmitter
, JSON.stringify
事件数据,通过websockets连接发送,在客户端将其改为事件,然后对客户端的事件采取行动。
The idea is that I want to grab all of the events spit out by a server side EventEmitter
, JSON.stringify
the event data, send it across a websockets connection, reform them on the client side as an event, and then act on the event on the client side.
推荐答案
正如所提到的,这种行为不在node.js核心中您可以使用hij1nx的EventEmitter2:
As mentioned this behavior is not in node.js core. But you can use hij1nx's EventEmitter2:
https:// github。 com / hij1nx / EventEmitter2
它不会使用EventEmitter破坏任何现有的代码,但增加了对命名空间和通配符的支持,例如:
It won't break any existing code using EventEmitter, but adds support for namespaces and wildcards. For example:
server.on('foo.*', function(value1, value2) {
console.log(this.event, value1, value2);
});
这篇关于收听Node.js中的所有发布的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!