Note that there are some explanatory texts on larger screens.

plurals
  1. POOverriding socket.io's emit and on?
    primarykey
    data
    text
    <p>During development, it helps me greatly to be able to see what packets arrive and gets sent. This is possible on the server side with logger. On the client end, however, there is no logger. I find myself to be littering console.log all over the place. </p> <p>Is it possible to override socket.emit and socket.on with console.log(arguments)? If I can override this at the before my socket, it would be really elegant. </p> <p>Somebody advised me to override the Parser instead. </p> <p>What's your 2cents on this? </p> <p><strong>EDIT</strong></p> <p>I tried Kato's suggestion and wrote the following: </p> <pre><code>var _origEmit = socket.emit; socket.emit = function() { console.log("SENT", Array.prototype.slice.call(arguments)); _origEmit.call(socket, arguments); }; </code></pre> <p>This works. However, Not so much with socket.on. My strategy is to wrap each callback with a console.log. If you know python, it's kind of like putting function decorators on the callbacks that console.log the arguments. </p> <pre><code>(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]); } </code></pre> <p>Any one can point to why monkey patching socket.on is not working? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload