ricvail Posted June 24, 2013 Share Posted June 24, 2013 (edited) 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 Edited June 24, 2013 by ricvail Quote Link to comment https://forums.phpfreaks.com/topic/279524-detecting-user-activity/ Share on other sites More sharing options...
kicken Posted June 24, 2013 Share Posted June 24, 2013 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/279524-detecting-user-activity/#findComment-1437685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.