Jump to content

Detecting user activity


Recommended Posts

Imagine a turn-based 1 vs 1 browsergame.

I need to be able to detect when a user goes offline (exits the browser, shuts down the PC, disables JS), and perform some actions.

Since I already have to costantly poll the server with AJAX to get the data, I thought that maybe I could add a line to my code that records in the DB the current time, and check if the other user's last connection time is < than (current time - 3 seconds), in wich case I consider him offline and declare the first user winner (add exp and money etc.)

But, what if one user exits, and after 1 second, the other does the same? Then he does not have time to detect the other user's logout. Nobody gets the Exp, but, most important, the entry in the "active fights" database does not get deleted.

Any ideas?

EDIT: Cron-jobs are a no-go, since Altervista, my PHP hosting website, requires me to pay for them and I can execute them every 60 minutes at best, while I would need them to run max every 5 seconds or so

Link to comment
Share on other sites

Timing out the user as you described is the only way to effectively determine if they have gone offline. You just need to make sure your timeout code handles all the possible scenarios.


It sounds like what you are talking about is an instance where both users timeout at the same time, so there is no clear winner because you can't determine who timed out first. What you need to do is just decide what you want to do in such a case, and code that. A few possible options are

1) Consider the fight a draw

2) Consider the fight void and just remove it.

3) Consider who last made a successful move the winner


Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.