开发者

Check if user is offline

I have an online game. I wish to show how many user are online. The problem is to know when a user is offline. Is there a way to perform a check on sessions cookie to acknowledge whether the session w开发者_StackOverflowith the broswer was closed? I was thinking about simply set a timeout on the server which launch a script that count how many session cookie are present, but how do I check if the session cookie is about somebody who's logged and not just a visitor?

How did you handle this?

1) I don't want to rely on a script fired with the logout button, since nobody ever logout... people simply close the browser.

2) About timestamps and registering activity? Since in my game users interact with an svg (not moving through pages), they generate a huge amount of clicks. Making a query for each click for each of them refreshing a record would be very expensive.


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

If it is longer than 30 mins or so, you can assume they are offline.

You can also explicitly set someone to offline when they click logout.

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

Whilst they are on the page, use setInterval() to extend the expiry date, up to a maximum range (in case the user leaves their browser window open for hours on end).


Since your code gets executed when the page is loaded you cannot make a check if the user closed his browser or not.

So the common approach would be to use timestamps and update this stamp if the user does something on your site and if the timestamp is older than say 5 minutes you just assume he is offline

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜