如何在 Grails 或 Java Application 中轻松实现“谁在线”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3271676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to easily implement "who is online" in Grails or Java Application?
提问by fabien7474
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?".
我正在 grails 中构建一个社区网站(使用 Apache Shiro 进行安全和身份验证系统),我想实现“谁在线?”功能。
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.
这个网址http://cksource.com/forums/viewonline.php(如果您无法访问此网址,请参阅下面的快照)给出了我想要实现的示例。
How can I do that in the most simple way? Is there any existing solution in Grails or in Java ?
我怎样才能以最简单的方式做到这一点?Grails 或 Java 中是否有任何现有的解决方案?
Thank you.
谢谢你。
Snapshot : Snapshot of Who is online page http://www.freeimagehosting.net/uploads/th.2de8468a86.pngor see here : http://www.freeimagehosting.net/image.php?2de8468a86.png
快照:谁在线页面的快照 http://www.freeimagehosting.net/uploads/th.2de8468a86.png或在这里查看:http: //www.freeimagehosting.net/image.php?2de8468a86.png
回答by BalusC
You need to collect all logged in users in a Set<User>in the applicationscope. Just hook on loginand logoutand add and remove the Useraccordingly. Basically:
您需要Set<User>在应用范围内收集所有登录的用户。只需钩上login并相应地logout添加和删除User。基本上:
public void login(User user) {
// Do your business thing and then
logins.add(user);
}
public void logout(User user) {
// Do your business thing and then
logins.remove(user);
}
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.
如果您将登录用户存储在会话中,那么您希望在会话销毁上添加另一个钩子以对任何登录用户发出注销。我不确定 Grails 是如何适应图片的,但是在 Java Servlet API 中,您可能希望使用HttpSessionListener#sessionDestroyed()它。
public void sessionDestroyed(HttpSessionEvent event) {
User user = (User) event.getSession().getAttribute("user");
if (user != null) {
Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins");
logins.remove(user);
}
}
You can also just let the Usermodel implement HttpSessionBindingListener. The implemented methods will be invoked automagically whenever the Userinstance is been put in session or removed from it (which would also happen on session destroy).
您也可以让User模型实现HttpSessionBindingListener. 每当User实例被放入会话或从中删除(这也会在会话销毁时发生)时,实现的方法将自动调用。
public class User implements HttpSessionBindingListener {
@Override
public void valueBound(HttpSessionBindingEvent event) {
Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins");
logins.add(this);
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins");
logins.remove(this);
}
// @Override equals() and hashCode() as well!
}
回答by Stefan Armbruster
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
这已经在邮件列表中讨论过:http: //grails.1312388.n4.nabble.com/Information-about-all-logged-in-users-with-Acegi-or-SpringSecurity-in-Grails- td1372911.html

