Instantly share code, notes, and snippets.

@sorah /a.js
Created Sep 9, 2012

Embed
What would you like to do?
listen to any events on socket.io / node EventEmitter; public domain own your risk
var SocketIO = require('socket.io')
, HTTP = require('http');
var orig_emit = SocketIO.Socket.prototype.$emit // http://nodejs.org/api/events.html#events_emitter_emit_event_arg1_arg2
SocketIO.Socket.prototype.$emit = function() {
orig_emit.apply(this, arguments)
var args = Array.prototype.slice.call(arguments);
var event = args.shift();
orig_emit.apply(this, ['anything', event, args]);
};
var app = HTTP.createServer(function (request, response) {
response.writeHead(404, {});
response.end('');
});
app.listen(config.app);
var io = SocketIO.listen(app);
io.sockets.on('connection', function (socket) {
socket.on('anything', function (event, args) {
console.log("event: ", event, args);
});
});
(function() {
var orig_emit = SocketIO.Socket.prototype.$emit // http://nodejs.org/api/events.html#events_emitter_emit_event_arg1_arg2
SocketIO.Socket.prototype.$emit = function() {
orig_emit.apply(this, arguments)
var args = Array.prototype.slice.call(arguments);
var event = args.shift();
orig_emit.apply(this, ['anything', event, args]);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment