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

前端 未结 14 1930
时光取名叫无心
时光取名叫无心 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:40

    It looks like the socket.io library stores these in a dictionary. As such, don't think this would be possible without modifying the source.

    From source:

    EventEmitter.prototype.on = function (name, fn) {
        if (!this.$events) {
          this.$events = {};
        }
    
        if (!this.$events[name]) {
          this.$events[name] = fn;
        } else if (io.util.isArray(this.$events[name])) {
          this.$events[name].push(fn);
        } else {
          this.$events[name] = [this.$events[name], fn];
        }
    
        return this;
      };
    

提交回复
热议问题