socket.io send packet to sender only

后端 未结 3 760
暗喜
暗喜 2021-01-30 18:16

I have yet to figure out how to directly respond to only the sender using socket.io

I have learned that io.sockets.emit sends to all clients but I wont to send informati

3条回答
  •  别跟我提以往
    2021-01-30 18:44

    late but better than never ;)

    I had smth like this:

    io.on('connection', function(socket){
        socket.emit('some event', data);
        socket.on('private event', function(message){
            this.emit('other private event', message);
        }
    }
    

    After some testing, I realized that 'this.emit' inside 'private event' closure send back only to sender. So i tried 'this.broadcast.emit', which send the message to all connected except the sender. I believe this is what you want from your code.

提交回复
热议问题