Jean Bon
Jean Bon

Reputation: 45

How to listen all action in nodejs

I'd like to know if it's possible to have a nodejs event which is listening all action sent by nodejs without modifying the lib source.

For example, i've got 2 actions :

socket.on("login", function() { //do something });
socket.on("getUser", function() { //do something });

But if socket sent me an action except "login" or "getUser", i'd like to do something like this :

socket.on("actionUnknow", function() { socket.disconnect(); });

I've found some topics on stackoverflow with the same problem, but answers was to modify lib source and I don't want to do it.

Upvotes: 0

Views: 230

Answers (1)

mscdex
mscdex

Reputation: 106696

The EventEmitter implementation used by node.js does not support wildcards of any kind, so basically you are out of luck. You could monkey-patch socket.emit, but that's more of a hack than anything:

var origEmit = socket.emit;
socket.emit = function(ev) {
  console.log(ev);
  origEmit.apply(this, arguments);
});

Upvotes: 2

Related Questions