2

I'm developing an app using socket io but my diconnect trigger event isn't working, I did everything like the docs say and it's still the same... It was working a few days ago but now it's not working anymore, I didn't edit any code since then

var io = require('socket.io')(server); io.on('event', function(data){}); var socketUsers = []; io.use(sharedSession(session, { autoSave: true })); io.on('connection', function(socket) { socketUsers.push(socket); console.log('Player connected: ' + socket.id); console.log('Connected players: ' + socketUsers.length); io.on('disconnect', function() { for(var i = 0; i < socketUsers.length; i++) { console.log(socketUsers[i].id); if(socketUsers[i].id == socket.id) { //delete socketUsers[i]; socketUsers.splice(i); break; } } console.log(socketUsers.length + ' players online.'); }); io.on('chat message', function(msg){ io.emit('chat message', msg); console.log('message: ' + msg); }); function broadcast(key, value) { io.emit(key, value); } }); 

1 Answer 1

3

You're attaching the disconnect event handler to the server instance (io) and not the socket instance (socket).

So try this instead:

socket.on('disconnect', function() { ... }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.