Overriding socket.io's emit and on?

前端 未结 4 594
礼貌的吻别
礼貌的吻别 2020-12-07 19:30

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, the

4条回答
  •  萌比男神i
    2020-12-07 19:55

    To override socket.on you actually need to override socket.$emit.

    Following example works both client and 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);
      };
    })();
    

提交回复
热议问题