Who's Online in Pylons

非 Y 不嫁゛ 提交于 2019-12-01 04:09:25

There's several ways you can do it, depending on how accurate you want to be.

The easiest, and first way to do it, use memecached or a persistent store to keep track of users, and the last time they hit a page. Consider anyone that has hit a page within X minutes to be 'logged in', as long as they're not 'logged out'. If you just update the db every page hit, or update memcached (or faster db), then query it for sessions within the last minute.

Second, but more intensive on your server, put Javascript on every page, that hits a special URL on your site every XX seconds/minutes to indicate the user is logged in. Record that, and use that count. This has the advantage that your JS could keep the counter on the page up to date in real-time. Though the polling might be expensive on your server.

Or third, use a Comet style system, say with Orbited, and as long as the user is on a page connected to the server, the connection will be open, and you can track how many open sessions you have. Orbited can handle around 10k open connections at a time per server I believe.

I'd recommend the first type as it requires the least extra overhead/setup and handles the fairly common case. If you're using real-time chat where you need more accuracy consider one of the other two.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!