关于判断openfire 服务器中用户的在线状态问题

    虽然接触了openfire有段时间了,但还是没有深入去理解其原理,所以,遇到一些新问题就有点束手无策,今天又遇到一个,开始以为用户的在线状态会存数据库里面呢,没想到openfire没这么干,存在了session里面,找了很多资料,终于找到了方法,好像也还很简单的。废话不多说,进入正题:

        首先我们要确保openfire 服务器中安装了presence 插件,同时在openfire后台中的服务器设置中进入Presence Service处,设置任何人都可以访问的权限(不过这样设置的话会对安全性有一定影响)。

        然后就是代码判断了:

    //传入路径格式:  String url = "http://localhost:9090/plugins/presence/status?jid=admin@meson.com&type=xml";

      public static int IsUserOnLine(String strUrl) {
        int state = 0;
        //返回值 : 0 - 用户不存在; 1 - 用户在线; 2 - 用户离线
        try {
            URL oUrl = new URL(strUrl);
            URLConnection oConn = oUrl.openConnection();
            if (oConn != null) {
                BufferedReader oIn = new BufferedReader(new   InputStreamReader(oConn.getInputStream()));
                if (null != oIn) {
                    String strFlag = oIn.readLine();
                    //System.out.println(strFlag);
                    oIn.close();
                    if (strFlag.indexOf("type=\"unavailable\"") >= 0) {
                        state = 2;
                    }
                    if (strFlag.indexOf("type=\"error\"") >= 0) {
                        state = 0;
                    } else if (strFlag.indexOf("priority") >= 0 || strFlag.indexOf("id=\"") >= 0) {
                        state = 1;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return state;
    }   //这是网上copy的代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值