Listen to All Emitted Events in Node.js

后端 未结 12 1296
孤街浪徒
孤街浪徒 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:40

    I know this is a bit old, but what the hell, here is another solution you could take.

    You can easily monkey-patch the emit function of the emitter you want to catch all events:

    function patchEmitter(emitter, websocket) {
      var oldEmit = emitter.emit;
    
      emitter.emit = function() {
          var emitArgs = arguments;
          // serialize arguments in some way.
          ...
          // send them through the websocket received as a parameter
          ...
          oldEmit.apply(emitter, arguments);
      }
    }
    

    This is pretty simple code and should work on any emitter.

提交回复
热议问题