当用户断开与服务器的连接时,如何找到会话ID?
目前,我有一种丑陋的方法要求所有现有客户发回信息。
e.g。在服务器上:
socket.on('disconnect', function() {
// What’s the sessionid?
});
答案 0 :(得分:8)
连接时,您可以将任何数据附加到套接字:
var clients = {}
var client_id = 0;
io.sockets.on('connection', function (socket) {
socket.client_id = client_id; // or your `session_id`
socket.anyData = "foobar";
clients[client_id] = socket;
client_id++;
socket.on("disconnect", function() {
console.log(this.anyData) // prints: foobar
delete clients[this.client_id];
}
}