2017-02-25 55 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 

希望這有助於!