SocketIO:通过套接字 ID 断开客户端连接? [英] SocketIO: disconnect client by socket id?

查看:94
本文介绍了SocketIO:通过套接字 ID 断开客户端连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 SocketIO 和几个连接的节点服务器.如何根据 ID 断开现有套接字?有房间,每个房间都有一个管理员和其他连接.我的目标是使管理套接字能够根据其 ID 断开任何其他套接字.

I have a Node server with SocketIO and several connections. How is it possible to disconnect an existing socket based on its ID? There are rooms, an admin in each room and other connections. My goal is to enable the admin socket to make any other socket disconnect based on its ID.

一种解决方法是向要被踢的客户端发送消息以 ping 服务器返回:io.to(socketId).emit('commit-suicide!');,然后它返回一个 "I-am-so-suicid" 消息,服务器调用 socket.disconnect().现在这显然不是最佳解决方案...

A workaround would be to send a message to the client-to-be-kicked to ping the server back: io.to(socketId).emit('commit-suicide!');, then it sends back an "I-am-so-suicid" message and the server calls socket.disconnect(). Now this is clearly not the optimal solution...

推荐答案

在服务器端你可以访问 io.sockets.connected ,它保存了所有连接的套接字:

On server side you can access io.sockets.connected which holds all connected sockets:

io.sockets.connected[socketId].disconnect();

这篇关于SocketIO:通过套接字 ID 断开客户端连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆