Socket.io Client: respond to all events with one handler?

前端 未结 14 1928
时光取名叫无心
时光取名叫无心 2020-11-28 04:16

Is it possible to have a socket.io client respond to all events without to have specify each event individually?

For example, something like this (which obviously do

14条回答
  •  [愿得一人]
    2020-11-28 04:39

    @Matthias Hopf answer

    Updated answer for v1.3.5. There was a bug with args, if you wanna listen on old event and * event together.

    var Emitter = require('events').EventEmitter;
    var emit = Emitter.prototype.emit;
    // [...]
    var onevent = socket.onevent;
    socket.onevent = function (packet) {
        var args = packet.data || [];
        onevent.call (this, packet);    // original call
        emit.apply   (this, ["*"].concat(args));      // additional call to catch-all
    };
    

提交回复
热议问题