Michał Lipa Posted June 1, 2017 Share Posted June 1, 2017 Do you have idea how to check if user closed game? I'm creating a multiplayer game and I need to trigger socket when user left game. I tried with these code in create(): window.onbeforeunload = function(e) { socket.emit('leaveGame', playerName); }; but these seems not to work Link to comment Share on other sites More sharing options...
mattstyles Posted June 1, 2017 Share Posted June 1, 2017 `onbeforeunload` should fire when the browser closes but you have no guarantee stuff inside will complete before the window is gone. Polling was often implemented to do this, the client sends a ping periodically, if the server does not receive one then either the client has crashed or been removed. Most socket implementations know when the socket has been closed though so you should use that as it'll be more reliable. Socket.io implement this as either an 'end' or a 'disconnect' event, I forget which. You don't need the client to tell the server it has disconnected, the connection dies and an event will be fired that can be reacted to. Link to comment Share on other sites More sharing options...
Michał Lipa Posted June 1, 2017 Author Share Posted June 1, 2017 I tried using socket 'disconnect' event but the problem is that I don't know how to identify user who already disconnected. client.on('joinGame', function(player){ console.log(player.id + ' joined the game'); connectCounter++; }); client.on('disconnect', function() { connectCounter--; //How to get disconnected player id? console.log('player disconnected -- closed socket'); }); Link to comment Share on other sites More sharing options...
mattstyles Posted June 1, 2017 Share Posted June 1, 2017 Oh, I see, you can grab the socket from the connection event, it should have an `id` associated with it (probably id), something like: var clients = [] io.on('connection', socket => { console.log(socket.id) clients.push(socket.id) socket.on('disconnect', () => { clients = clients.filter(id => id !== socket.id) }) }) By trapping it in a closure you have access to the raw socket, you could store each socket connection in an array (or whatever) somewhere else if you wanted and create some sort of wrapper function that passes that id to a disconnect callback if you wanted. I'm being pretty specific to socket.io implementation here (as its what I have most experience with), not passing the socket or an id through to the disconnect callback is a little odd, other socket implementations might have a better api in this regard. Link to comment Share on other sites More sharing options...
Arcanorum Posted June 1, 2017 Share Posted June 1, 2017 When a client disconnects, socket.io will automatically detect that they have disconnected and will fire the 'disconnect', and 'disconnecting' events. The only difference is that using the 'disconnecting' event will allow you to do stuff with the rooms the socket is in before it is removed from them. You could use an either an object or array to store a list of connected clients, identifiable by the ID of the socket connection for that client. Depends on how you like to access the client data. var clients = {}; io.on('connection', function(socket){ // Add the client that just connected to the list of active clients, using the ID // of the socket as the key to access the player data object for that client. clients[socket.id] = {id: '5a4b3c2d1', username: 'blahblah', hitPoints: 80 /*, etc: 'etc'*/}; socket.on('disconnecting', function(){ // Remove the client from the list of clients. delete clients[socket.id]; }); }) mattstyles 1 Link to comment Share on other sites More sharing options...
Recommended Posts