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

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

    Finally, there is a module called socket.io-wildcard which allows using wildcards on client and server side

    var io         = require('socket.io')();
    var middleware = require('socketio-wildcard')();
    
    io.use(middleware);
    
    io.on('connection', function(socket) {
      socket.on('*', function(){ /* … */ });
    });
    
    io.listen(8000);
    
    0 讨论(0)
  • 2020-11-28 04:37

    There is a long discussion about this topic going on at the Socket.IO repository issue page. There are a variety of solutions posted there (e.g., overriding EventEmitter with EventEmitter2). lmjabreu released another solution a couple weeks ago: a npm module called socket.io-wildcard that patches in a wildcard event onto Socket.IO (works with the current Socket.IO, ~0.9.14).

    0 讨论(0)
  • 2020-11-28 04:38

    Even though this is a old question, I have the same problem and solved using the native socket in Node.js, which has a event of .on('data'), fired everytime some data comes. So this is what I've done so far:

    const net = require('net')
    
    const server = net.createServer((socket) => {
        // 'connection' listener.
        console.log('client connected')
    
        // The stuff I was looking for
        socket.on('data', (data) => {
            console.log(data.toString())
        })
    
        socket.on('end', () => {
            console.log('client disconnected')
        })
    })
    
    server.on('error', (err) => {
        throw err;
    })
    
    server.listen(8124, () => {
        console.log('server bound');
    })
    
    0 讨论(0)
  • 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
    };
    
    0 讨论(0)
  • 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;
      };
    
    0 讨论(0)
  • 2020-11-28 04:40

    Updated solution for socket.io-client 1.3.7

    var onevent = socket.onevent;
    socket.onevent = function (packet) {
        var args = packet.data || [];
        onevent.call (this, packet);    // original call
        packet.data = ["*"].concat(args);
        onevent.call(this, packet);      // additional call to catch-all
    };
    

    Use like this:

    socket.on("*",function(event,data) {
        console.log(event);
        console.log(data);
    });
    

    None of the answers worked for me, though the one of Mathias Hopf and Maros Pixel came close, this is my adjusted version.

    NOTE: this only catches custom events, not connect/disconnect etc

    0 讨论(0)
提交回复
热议问题