如何轻松实现“谁在线”功能在 Grails 或 Java 应用程序中?
我正在 grails 中构建一个社区网站(使用 Apache Shiro 进行安全和身份验证系统),我想实现“谁在线?”功能。
此网址http://cksource.com/forums/viewonline.php(如果您无法访问此网址)给出了我想要实现的目标的示例。
我怎样才能以最简单的方式做到这一点? Grails 或 Java 中是否有任何现有的解决方案?
谢谢。
快照 : 谁在线页面快照 http://www.freeimagehosting.net/uploads/th .2de8468a86.png 或参见此处:http://www.freeimagehosting.net /image.php?2de8468a86.png
I am building a community website in grails (using Apache Shiro for security and authentication system) and I would like to implement the feature "who is online?".
This url http://cksource.com/forums/viewonline.php (see snapshot below if you do not have acess to this Url) gives an example of what I would like to achieve.
How can I do that in the most simple way? Is there any existing solution in Grails or in Java ?
Thank you.
Snapshot : Snapshot of Who is online page http://www.freeimagehosting.net/uploads/th.2de8468a86.png or see here : http://www.freeimagehosting.net/image.php?2de8468a86.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 application 范围内的
Set
中收集所有登录用户。只需挂钩login
和logout
并相应地添加和删除User
即可。基本上:如果您将登录用户存储在会话中,那么您需要在会话销毁上添加另一个挂钩,以对任何登录用户发出注销。我不确定 Grails 如何适应这种情况,但是在 Java Servlet API 中,您希望使用
HttpSessionListener#sessionDestroyed()
为此。您还可以让
User
模型实现HttpSessionBindingListener
。每当将User
实例放入会话中或从会话中删除(这也会在会话销毁时发生)时,将自动调用已实现的方法。You need to collect all logged in users in a
Set<User>
in the application scope. Just hook onlogin
andlogout
and add and remove theUser
accordingly. Basically:If you're storing the logged-in users in the session, then you'd like to add another hook on session destroy to issue a logout on any logged-in user. I am not sure about how Grails fits in the picture, but talking in Java Servlet API, you'd like to use
HttpSessionListener#sessionDestroyed()
for this.You can also just let the
User
model implementHttpSessionBindingListener
. The implemented methods will be invoked automagically whenever theUser
instance is been put in session or removed from it (which would also happen on session destroy).这已经在邮件列表上讨论过:http://grails.1312388.n4.nabble.com/Information-about-all-logged-in-users-with-Acegi-or-SpringSecurity- in-Grails-td1372911.html
This has been discussed some time ago on the mailing list: http://grails.1312388.n4.nabble.com/Information-about-all-logged-in-users-with-Acegi-or-SpringSecurity-in-Grails-td1372911.html