Check if user is disconnected

I have an online game. I want to show how many users are online. The problem is to know when the user is offline. Is there a way to do session cookie verification to confirm if the session with the browser is closed? I was thinking of just setting a timeout on a server that runs a script that counts how many cookies are present, but how to check if a session cookie is someone who signed up, not just a visitor?

How did you handle this?

1) I do not want to rely on a script running with the exit button, since no one ever logs out ... people just close the browser.

2) About timestamps and recording activities? Since in my game users interact with svg (without moving through the pages), they generate a huge number of clicks. Running a query for every click for each of them refreshing a record will be very expensive.

+4
source share
2 answers

When a user interacts with the site, set the last activity time.

If it is more than 30 minutes or so, you can assume that they are offline.

You can also explicitly disconnect someone offline when they log out.

However, your case is a little different. You can use the heartbeat script style.

While they are on the page, use setInterval() to extend the validity period to the maximum range (in case the user leaves his browser window for several hours in a row).

+4
source

Since your code starts when the page loads, you cannot check if the user has closed his browser or not.

So the general approach is to use timestamps and update this mark if the user is doing something on your site and if the time stamp is older than 5 minutes, you just think that he is offline

+2
source

Source: https://habr.com/ru/post/1347703/


All Articles