Reorient socket.io to emit and continue?

During development, it helps me a lot to see which packets arrive and go. This is possible on the server side using the registrar. However, there is no registrar on the client side. I find that console.log trash is all over the place.

Is it possible to override socket.emit and socket.on using console.log (arguments)? If I can override this on my socket, it will be very elegant.

Someone advised me to override the Parser instead.

What are your 2cents on this?

EDIT

I tried Kato's suggestion and wrote the following:

var _origEmit = socket.emit; socket.emit = function() { console.log("SENT", Array.prototype.slice.call(arguments)); _origEmit.call(socket, arguments); }; 

It works. However, not much with socket.on. My strategy is to wrap each callback with console.log. If you know python, it is like putting function decorators on callbacks that have console.log arguments.

 (function(socket) { var _origOn = socket.on; socket.on = function() { var args = Array.prototype.slice.call(arguments) , handlerType = args[0] , originalCallback = args[1]; var wrappedCallback = function() { // replace original callback with a function // wrapped around by console.log console.log("RECEIVED", Array.prototype.slice.call(arguments)); originalCallback.call(socket, arguments); } _origOn.call(socket, [handlerType, wrappedCallback]); } 

Can anyone point out why the monkey patching socket.on is not working?

+20
Jan 12 '12 at 9:05
source share
4 answers

To override socket.on , you really need to override the socket . $ emit .

The following example works both on the client side and on the server side (tested on socket.io 0.9.0):

 (function() { var emit = socket.emit; socket.emit = function() { console.log('***','emit', Array.prototype.slice.call(arguments)); emit.apply(socket, arguments); }; var $emit = socket.$emit; socket.$emit = function() { console.log('***','on',Array.prototype.slice.call(arguments)); $emit.apply(socket, arguments); }; })(); 
+55
Mar 12 '12 at 20:23
source share

Works, tested:

 var _emit = socket.emit; _onevent = socket.onevent; socket.emit = function () { //Override outgoing //Do your logic here console.log('***', 'emit', arguments); _emit.apply(socket, arguments); }; socket.onevent = function (packet) { //Override incoming var args = packet.data || []; //Do your logic here console.log('***', 'onevent', packet); _onevent.call(socket, packet); }; 
+5
Oct 20 '15 at 6:16
source share

There is a module called socket.io-wildcard , which allows you to use wildcards on the client and server side, no need to rewrite anything

 var io = require('socket.io')(); var middleware = require('socketio-wildcard')(); io.use(middleware); io.on('connection', function(socket) { socket.on('*', function(){ /* … */ }); }); io.listen(8000); 
+3
May 11 '16 at 10:08
source share
 <script src="/socket.io/socket.io.js"></script> <script> (function() { var _origEmit = socket.emit; socket.emit = function() { console.log(arguments); _origEmit.apply(null, Array.prototype.slice.call(arguments)); }; })(); </script> 
0
Jan 12 2018-12-12T00:
source share



All Articles