如何轻松实现“谁在线"在 Grails 或 Java 应用程序中? [英] How to easily implement "who is online" in Grails or Java Application?

查看:29
本文介绍了如何轻松实现“谁在线"在 Grails 或 Java 应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 grails 中构建一个社区网站(使用 Apache Shiro 进行安全和身份验证系统),我想实现谁在线?"功能.

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?".

这个网址 http://cksource.com/forums/viewonline.php(见快照下面(如果您无法访问此网址)给出了我想要实现的示例.

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.

我怎样才能以最简单的方式做到这一点?Grails 或 Java 中是否有任何现有的解决方案?

How can I do that in the most simple way? Is there any existing solution in Grails or in Java ?

谢谢.

快照:谁在线页面的快照 http://www.freeimagehosting.net/上传/th.2de8468a86.png 或在这里查看:http://www.freeimagehosting.net/image.php?2de8468a86.png

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

推荐答案

您需要在 application 范围内的 Set 中收集所有已登录的用户.只需挂钩 loginlogout 并相应地添加和删除 User.基本上:

You need to collect all logged in users in a Set<User> in the application scope. Just hook on login and logout and add and remove the User accordingly. Basically:

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);
}

如果您将登录用户存储在会话中,那么您希望在会话销毁上添加另一个钩子,以对任何登录用户发出注销.我不确定 Grails 如何适合图片,但在 Java Servlet API 中,您想使用 HttpSessionListener#sessionDestroyed() 为此.

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.

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);
    }
}

您也可以让 User 模型实现 HttpSessionBindingListener.每当 User 实例被放入会话或从中删除(这也会在会话销毁时发生)时,实现的方法将自动调用.

You can also just let the User model implement HttpSessionBindingListener. The implemented methods will be invoked automagically whenever the User instance is been put in session or removed from it (which would also happen on session destroy).

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!

}

这篇关于如何轻松实现“谁在线"在 Grails 或 Java 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆