force client disconnect from server with socket.io

2019-01-07 07:07发布

Is there any way to disconnect a client with SocketIO, and literally close the connection? So if someone is connected to my server, and I want to close the connection between them and my server, how would I go about doing that?

标签: socket.io
12条回答
ゆ 、 Hurt°
2楼-- · 2019-01-07 07:30

In my case I wanted to tear down the underlying connection in which case I had to call socket.disconnect(true) as you can see is needed from the source here

查看更多
【Aperson】
3楼-- · 2019-01-07 07:36

Any reason why you can't have the server emit a message to the client that makes it call the disconnect function?

On client:

socket.emit('forceDisconnect');

On Server:

socket.on('forceDisconnect', function(){
    socket.disconnect();
});
查看更多
时光不老,我们不散
4楼-- · 2019-01-07 07:38

For those who found this on google - there is a solution for this right now:

Socket.disconnect() kicks the client (server-side). No chance for the client to stay connected :)

查看更多
Viruses.
5楼-- · 2019-01-07 07:40

I'm using client.emit('disconnect') + client.removeAllListeners() for connected client for ignore all events after disconnect

查看更多
劫难
6楼-- · 2019-01-07 07:46

Edit: This is now possible

You can now simply call socket.disconnect() on the server side.

My original answer:

This is not possible yet.

If you need it as well, vote/comment on this issue.

查看更多
Lonely孤独者°
7楼-- · 2019-01-07 07:50

Socket.io uses the EventEmitter pattern to disconnect/connect/check heartbeats so you could do. Client.emit('disconnect');

查看更多
登录 后发表回答