Socket.IO 1.0.x: Get socket by id

大憨熊 提交于 2019-12-29 08:27:25

问题


In the 0.9.x version, we can get socket by ID like this:

io.sockets.socket(socketId)

But in 1.0.x we can't. How to find a socket by id in 1.0.x?


回答1:


For socket.io 1.0 use:

io.sockets.connected[socketId]

For 0.9 its io.sockets.sockets[socketId] and not io.sockets.socket[socketId]




回答2:


you can also use like:

io.to(socketid).emit();



回答3:


Socket.io Version 2.0.3+

    let namespace = null;
    let ns = _io.of(namespace || "/");
    let socket = ns.connected[socketId] // assuming you have  id of the socket



回答4:


The most simlple way I tried is:

var socket1 = io.of("/").connected[socketId];

Then you can do

socket1.join("some room");
socket1.leave("some room");
socket1.emit();

or other things you want.



来源:https://stackoverflow.com/questions/27055989/socket-io-1-0-x-get-socket-by-id

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