Listen to All Emitted Events in Node.js

后端 未结 12 1325
孤街浪徒
孤街浪徒 2020-12-24 00:06

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(\         


        
12条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 00:51

    Here's a debug tool inspired by Martin's answer (https://stackoverflow.com/a/18087021/1264797). I just now used this to figure out what was going wrong in a set of streams by logging all of their events to the console. Works great. As Martin illustrates, OP could use it by replacing the console.log() call with a websocket sender.

    function debug_emitter(emitter, name) {
        var orig_emit = emitter.emit;
        emitter.emit = function() {
            var emitArgs = arguments;
            console.log("emitter " + name + " " + util.inspect(emitArgs));
            orig_emit.apply(emitter, arguments);
        }
    }
    

提交回复
热议问题