I've been trying to wrap my head around this for a few days, and I just don't understand it. I want to kill my session, when I leave the webpage, but the exception I'm getting (which crashes the server) is:
TypeError: Cannot read property 'destroy' of undefined
at Socket.<anonymous> (/home/ubuntu/workspace/server.js:46:20)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.emit (/home/ubuntu/workspace/node_modules/socket.io/lib/socket.js:128:10)
at Socket.onclose (/home/ubuntu/workspace/node_modules/socket.io/lib/socket.js:425:8)
at Client.onclose (/home/ubuntu/workspace/node_modules/socket.io/lib/client.js:232:24)
at emitTwo (events.js:92:20)
at Socket.emit (events.js:172:7)
at Socket.onClose (/home/ubuntu/workspace/node_modules/socket.io/node_modules/engine.io/lib/socket .js:304:10)
at WebSocket.g (events.js:260:16)
My connection with disconnect:
// create socket.io connection
io.on('connection', function(socket) {
if(sess != null) {
list.push(sess);
}
if(list != null) {
for(var i = 0; i <= list.length; i++) {
//console.log("User with session id connected: " +sess['sesid']);
console.log(JSON.stringify(sess['sesid']) + " CONNECTED");
}
}
socket.on('disconnect', function(req){
req.session.destroy(sess.sesid);
//destroy session
console.log('session killed');
});
Anyone got a clue on how to do this easier? I'm pretty much just adding each new connection into a list of sessions where I save the sessionID and in the future a 'username' to differentiate current online users, to then remove the use when they disconnect.