Sign in to follow this  
Michał Lipa

How to detect if user is closing game

Recommended Posts

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

Share this post

Link to post
Share on other sites

`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. 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.

Share this post

Link to post
Share on other sites

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( + ' joined the game');


client.on('disconnect', function() {
   //How to get disconnected player id?
   console.log('player disconnected -- closed socket');


Share this post

Link to post
Share on other sites

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 => {

  socket.on('disconnect', () => {
    clients = clients.filter(id => 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 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.

Share this post

Link to post
Share on other sites

When a client disconnects, 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[] = {id: '5a4b3c2d1', username: 'blahblah', hitPoints: 80 /*, etc: 'etc'*/};
	socket.on('disconnecting', function(){
		// Remove the client from the list of clients.
		delete clients[];



Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.