0

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.

ssjturtle
  • 11
  • 1
  • 1
  • Have you tried just setting the object to `null`? – pay Jul 05 '16 at 16:21
  • Not yet, but the thing is the list has to keep all the current online users listed. so setting the list to null would remove everything right? – ssjturtle Jul 05 '16 at 16:26
  • Yes, but it is saying your `session` object is undefined. What you're saying at the bottom of your post sounds right, you should likely remove them individually when they disconnect in the `disconnect` event – pay Jul 05 '16 at 16:29
  • that's exactly what I'm trying to do, but as soon as I call for the req.session.destroy() it then says destroy() is undefined, which I find odd. – ssjturtle Jul 05 '16 at 16:33
  • socket.io documentation is pretty scarce, did you see that function somewhere? You'll have to use something else to remove the user from the list I'd imagine – pay Jul 05 '16 at 16:34
  • the session.destroy() function is a part of express-session. I tried setting the list to null when I dispose of a session (for testing) and then if a new item should be set to be added into the list it then crashes saying it can't read property type null from list. as expected – ssjturtle Jul 05 '16 at 16:36
  • Take a look here: http://stackoverflow.com/questions/9918203/remove-objects-on-disconnect-socket-io and here: http://stackoverflow.com/questions/24463447/socket-io-disconnect-client-by-id as that might be helpful – pay Jul 05 '16 at 16:40
  • @pay thanks, it seemed to have fixed it somehow. I think instead of sessions, I'll use the ID the client gives the server automatically. Seeing as I don't need to save any settings/anything the user sets up. Thanks! – ssjturtle Jul 05 '16 at 16:47

1 Answers1

0

thanks to pay (https://stackoverflow.com/users/1438893/pay) finding these 2 handy links to help, I've chosen to use the clientid and just removing that from the list of connected sockets.

Socket.io disconnect client by id

and

Remove objects on disconnect socket.io

Community
  • 1
  • 1
ssjturtle
  • 11
  • 1
  • 1