socket.IO: how can i get the socketid when i emit to a specific client in socket.io 1.0

元气小坏坏 提交于 2019-12-13 02:26:52

问题


I want to emit a action to a specific client using socket.io 1.0.

After reading Sending message to a specific ID in Socket.IO 1.0 , I know I can use:

io.sockets.connected[socketid].emit()

to emit a action to specific person.

But how can I get the socketid?

I used to write like this : socket.id=nickName, but it's wont work.


回答1:


Simply access id property of socket object:

io.on('connection', function(socket) {
    console.log(socket.id);
});


来源:https://stackoverflow.com/questions/24135188/socket-io-how-can-i-get-the-socketid-when-i-emit-to-a-specific-client-in-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!