Destroying a handshake after logout. socket.io

2019-04-13 00:17发布

问题:

Hello I am trying to build chat into an application. What I am wondering is when the user logs out of the website how do I also destroy the socket.io handshake associated with that session so the user cannot send messages from say another tab when he is logged out.

I am using expressjs if that is any help.

回答1:

Well in case anyone ever find this and wants to know I did figure it out. You can access the sockets disconnect function. I had object of users ids and their socket id so when someone logged out I called

app.get("/logout", function(req,res){
  //do other logging out stuff
  sockets.disconnectUser(req.session.user_id);
}

// Disconnect User function
sockets.disconnectUser = function(user_id){
  sockets.socket(users[user_id]).disconnect();
}