开发者

How to determine if certain user (not current_user) is online in restful_authentication?

I 开发者_StackOverflow中文版have to know whether user is online or not, not current authenticated user, but any other. Does restful_authentication has some methods to determine it?


The only way to know if somebody is online (and viewing your site) is to have an open connection to her. But, after a page is loaded the HTTP connection to your server is closed, and the client does not talk to your server again until she wants to see another page.

Given this, you can do the following:

  1. Take a guess if the client is still there by assuming that she's "online" for x minutes after she has last requested a page.
  2. Have the client send a "heartbeat" to your server in regular intervals using Javascript, then apply 1.
  3. Actually have the client keep a persistent connection to your server using something like Comet, which will give you the most accurate result.

What's most appropriate depends on how accurate you need the status to be.


If your users log in then you could add a column to your users table like

rails g migration add_online_to_users online:boolean

and every time a user starts a new session you could populate that column with 'true'

user.online = true

and when the user logs out you could put

user.online = false in your sessions destroy method

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜