Sending message to a specific ID in Socket.IO 1.0

后端 未结 6 974
天命终不由人
天命终不由人 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:15

    In socket.io 1.0 they provide a better way for this. Each socket automatically joins a default room by self id. Check documents: http://socket.io/docs/rooms-and-namespaces/#default-room

    So you can emit to a socket by id with following code:

    io.to(socketid).emit('message', 'for your eyes only');
    

提交回复
热议问题