Socket.IO 1.0.x: Get socket by id

2019-02-21 09:46发布

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?

3条回答
放荡不羁爱自由
2楼-- · 2019-02-21 10:29

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
查看更多
Summer. ? 凉城
3楼-- · 2019-02-21 10:30

For socket.io 1.0 use:

io.sockets.connected[socketId]

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

查看更多
来,给爷笑一个
4楼-- · 2019-02-21 10:36

you can also use like:

io.to(socketid).emit();
查看更多
登录 后发表回答