2017-02-25 51 views
0

我一直在尝试使用socketio实现聊天服务。我需要设置一个消息到选定的套接字ID。该文件说要使用像socket.to(<socketid>).emit('hey', 'I just met you'); 这样的功能,但它似乎没有工作。几天前我通过io.sockets.connected[<socketid>].emit('hey', 'I just met you');这样的功能实现了这个功能,但现在看起来似乎不再起作用了。顺便说一句socket.broadcast.emit();工作得很好。如何解决这个问题?Socketio发送给个人socketid(私人消息)

回答

0

您可以使用socket.broadcast.to(<socketid>)广播到特定的套接字ID。

下面是一个例子:

socket.join('my-unique-room-name'); 
socket.broadcast.to('my-unique-room-name').emit('welcome', 'helloWorld'); //only people in 'my-unique-room-name' will receive this message 

希望这有助于!