覆盖socket.io的发射和开启?

时间:2022-08-22 16:57:09

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.

在开发过程中,能够很好地查看数据包到达和发送的内容对我有很大帮助。这可以在带有记录器的服务器端进行。但是,在客户端,没有记录器。我发现自己在整个地方乱扔垃圾。

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.

是否可以使用console.log(参数)覆盖socket.emit和socket.on?如果我可以在我的套接字之前覆盖它,那将非常优雅。

Somebody advised me to override the Parser instead.

有人建议我改写Parser。

What's your 2cents on this?

你的2点是什么?

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);
};

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.

这很有效。但是,与socket.on没那么多。我的策略是用console.log包装每个回调。如果你知道python,它就像把函数装饰器放在console.log参数的回调上。

(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]);
}

Any one can point to why monkey patching socket.on is not working?

任何人都可以指出为什么猴子修补socket.on不工作?

4 个解决方案

#1


56  

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

要覆盖socket.on,你实际上需要覆盖socket。$ emit。

Following example works both client and server-side (tested on socket.io 0.9.0):

以下示例适用于客户端和服务器端(在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);
  };
})();

#2


5  

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);
    };

#3


3  

There is a module called socket.io-wildcard which allows using wildcards on client and server side, no need to overwrite anything anymore

有一个名为socket.io-wildcard的模块允许在客户端和服务器端使用通配符,不再需要覆盖任何东西

var io         = require('socket.io')();
var middleware = require('socketio-wildcard')();

io.use(middleware);

io.on('connection', function(socket) {
  socket.on('*', function(){ /* … */ });
});

io.listen(8000);

#4


0  

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

#1


56  

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

要覆盖socket.on,你实际上需要覆盖socket。$ emit。

Following example works both client and server-side (tested on socket.io 0.9.0):

以下示例适用于客户端和服务器端(在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);
  };
})();

#2


5  

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);
    };

#3


3  

There is a module called socket.io-wildcard which allows using wildcards on client and server side, no need to overwrite anything anymore

有一个名为socket.io-wildcard的模块允许在客户端和服务器端使用通配符,不再需要覆盖任何东西

var io         = require('socket.io')();
var middleware = require('socketio-wildcard')();

io.use(middleware);

io.on('connection', function(socket) {
  socket.on('*', function(){ /* … */ });
});

io.listen(8000);

#4


0  

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