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

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

问题描述

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



此网址 http://cksource.com/ forums / viewonline.php (如果您没有访问此Url,请参阅下面的快照)给出了一个我想达到的例子。



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

谢谢。



快照: Who is Snapshot of online page http://www.freeimagehosting.net/uploads/th.2de8468a86.png或在这里看到: http://www.freeimagehosting.net/image.php?2de8468a86 .png

解决方案

您需要收集 Set< User>中的所有登录用户; 应用程序范围内的。只需钩住 login logout 并添加并移除 User 相应地。基本上:

  public void login(User user){
//做你的生意,然后
logins.add(用户);
}

public void logout(User user){
//做你的生意,然后
logins.remove(user);

$ / code>

如果您将登录用户存储在会话中,那么您'想要在会话销毁中添加​​另一个挂钩以对任何登录用户发出注销。我不确定Grails如何适合图片,但是在Java Servlet API中讨论时,您想使用 HttpSessionListener#sessionDestroyed() 。 / p>

  public void sessionDestroyed(HttpSessionEvent event){
User user =(User)event.getSession()。getAttribute(用户);
if(user!= null){
Set< User> logins =(Set< User>)event.getSession()。getServletContext()。getAttribute(logins);
logins.remove(user);


$ / code>

您也可以让用户模型实现 了HttpSessionBindingListener 。只要用户实例被放入会话中或从中删除(这也会在会话销毁时发生),所实现的方法将被自动调用。

  public class User实现HttpSessionBindingListener {

@Override
public void valueBound(HttpSessionBindingEvent event){
Set< ;使用者> 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()和hashCode()也是如此!

}


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

解决方案

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

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

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天全站免登陆