openfire如何获取所有在线用户
2017-06-07 08:24
375 查看
openfire如何获取所有在线用户
博客分类: IM
想象中如此简单的功能,想不到却这般大费周折。
如要实现,必须先确保:
1. openfire中安装有“Presence” 插件。
![](http://img169.ph.126.net/8Mgwvjl6bi66ocmJRTdPxg==/1456070054525824785.jpg)
2. 确保该插件设置可允许任何人访问(如果是跨域浏览的话)
![](http://img618.ph.126.net/pCS0TqBHzbB5aEmDJL28qw==/1963006487581684530.jpg)
然后通过如下方式访问:http://www.igniterealtime.org/projects/openfire/plugins/presence/readme.html 。
访问结果如下:
账号 | 状态 | xml | text |
user1 | 不存在 | <presence type="error" from="user1@my.openfire.com"><error code="403" type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence> | null |
user2 | 离线 | <presence type="unavailable" from="user2@my.openfire.com"><status>Unavailable</status></presence> | Unavailable |
user8 | 在线 | <presence from="user8@my.openfire.com/trm"><priority>0</priority></presence> 或者 <presence id="6Mgiu-13" from="user8@my.openfire.com/Smack"/> | null |
java代码:
import java.net.*;
import java.io.*;
/**
* 判断openfire用户的状态
* strUrl : url格式 - http://my.openfire.com:9090/plugins/presence/status?jid=user1@my.openfire.com&type=xml
* 返回值 : 0 - 用户不存在; 1 - 用户在线; 2 - 用户离线
* 说明 :必须要求 openfire加载 presence 插件,同时设置任何人都可以访问
*/
public static short IsUserOnLine(String strUrl)
{
short shOnLineState = 0; //-不存在-
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();
oIn.close();
if(strFlag.indexOf("type=\"unavailable\"")>=0)
{
shOnLineState = 2;
}
if(strFlag.indexOf("type=\"error\"")>=0)
{
shOnLineState = 0;
}
else if(strFlag.indexOf("priority")>=0 || strFlag.indexOf("id=\"")>=0)
{
shOnLineState = 1;
}
}
}
}
catch(Exception e)
{
}
return shOnLineState;
}
转自:http://iammr.7.blog.163.com/blog/static/491026992010101635939962/
关于判断openfire 服务器中用户的在线状态问题
收藏
肖文锋
发表于 4年前
阅读 6577
收藏 5
点赞 1
评论 6
破译“粽”子代码,拿最高悬赏!>>>
![](https://my.oschina.net/img/hot3.png)
虽然接触了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的代码
到这里问题就算解决了,不过对于openfire,还有很多未知数等着解决....
openfire如何获取所有在线用户
2010-11-16 16:11:51| 分类: 挨踢咋活 | 标签:openfire 在线用户 |举报|字号 订阅下载LOFTER我的照片书 |
简单的代码如下
package com.my.openfire.plugin.online; import java.util.Collection; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.SessionManager; import org.jivesoftware.openfire.SessionResultFilter; import org.jivesoftware.openfire.session.ClientSession; import org.jivesoftware.openfire.session.Session; import org.jivesoftware.admin.AuthCheckFilter; import org.jivesoftware.util.WebManager; import com.my.openfire.plugin.OnlinePlugin; public class OnlineStatusServlet extends HttpServlet { private OnlinePlugin plugin; private ServletConfig servletConfig; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); this.servletConfig = servletConfig; plugin = (OnlinePlugin) XMPPServer.getInstance().getPluginManager().getPlugin("online"); // Exclude this servlet from requering the user to login AuthCheckFilter.addExclude("online/status"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); WebManager webManager = new WebManager(); HttpSession session = request.getSession(true); webManager.init(request, response, session, servletConfig.getServletContext()); SessionManager sessionManager = webManager.getSessionManager(); int sessionCount = sessionManager.getUserSessionsCount(false); SessionResultFilter filter = SessionResultFilter.createDefaultSessionFilter(); filter.setSortOrder(0); filter.setStartIndex(0); filter.setNumResults(sessionCount); Collection sessions = sessionManager.getSessions(filter); StringBuilder sb = new StringBuilder(); int nCount = 0; for (ClientSession sess : sessions) { String strTmp = sess.getAddress().toString(); { if(nCount>0) sb.append(","); sb.append(strTmp); nCount++; } } if(nCount==0) sb.append("null"); out.println(sb.toString()); out.flush(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void destroy() { super.destroy(); // Release the excluded URL AuthCheckFilter.removeExclude("online/status"); } } 最后通过:http://localhost:9090/plugins/online/status访问即可。返回用逗号分隔的用户id;如果没有在线用户,返回null。。
附记
实际上完成这样一个功能,需要攻克两个堡垒。如何获取在线用户
如何弄一个插件提供相应服务
实际过程中,方向明了,但是其中细节却颇费一点周折
前者,研究openfire admin控制台的“会话”栏目,找到对应的jsp代码研究即可。原理就是openfire的在线状态没有记在数据库中,而是通过连接session来实现的。将对应的功能先用一个jsp实现,然后测试通过。最后将jsp转移成servlet即可。说明一下:新加一个jsp页面,需要改动admin插件中的一些配置以识别该jsp。显然应该以插件的方式提供。
后者,怎么做插件?研究presence的源代码,照样copy一个;将presence的地方全部改名为online即可。可是自己在改的过程中,将插件的目录名并没有取成online,导致连续两天试验时总是404错误。真浪费时间啊。其实现在还没有明白插件的原理,只是生吞活剥的实现功能;封装为插件。可用即可。
jar: http://pan.baidu.com/s/1i9vA
src:http://pan.baidu.com/s/1tLygO
相关文章推荐
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- 在SQL Server中如何获取某用户对某个表中所有权限
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?
- WCF热门问题编程示例(5):WCF服务如何获取客户端在线用户数量?