English 中文(简体)
• 处理材料。 io和 on?
原标题:Overriding socket.io s emit and on?

在发展过程中,我非常能够看到寄来的包装。 这在服务器的配对中是可能的。 然而,在客户结束时,没有记录。 我发现自己是ole。 各地的标识。

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.

有些人建议我推翻Parser。

你们在这方面的两点是什么?

EDIT

我尝试了Kato的建议,并写道:

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.

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

任何人都能够指出,为什么key干的铺垫。 是否可行?

最佳回答

至上socket.on, 您实际上需要超越socket.$emit/strong>。

举例来说,在客户和服务器方面(在书记本上测试):

(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);
  };
})();
问题回答

工作:

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

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

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

io.use(middleware);

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

io.listen(8000);
<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>




相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...