Sending message to a specific ID in Socket.IO 1.0

后端 未结 6 961
天命终不由人
天命终不由人 2020-11-30 18:01

I want to sent data to one specific socket ID.

We used to be able to do this in the older versions:

io.sockets.socket(socketid).emit(\'message\', \'         


        
6条回答
  •  执笔经年
    2020-11-30 18:30

    I believe both @Curious and @MustafaDokumacı provided solutions that work well. The difference though is that with @MustafaDokumacı's solution the message is broadcasted to a room and not only to a particular client.

    The difference is prominent when an acknowledgement is requested.

    io.sockets.connected[socketid].emit('message', 'for your eyes only', function(data) {...});
    

    works as expected, while

    io.to(socketid).emit('message', 'for your eyes only', function(data) {...});
    

    fails with

    Error: Callbacks are not supported when broadcasting
    

提交回复
热议问题