openfire xmpp 如何判断用户是否在线
2013-02-26 12:20 4716人阅读 评论(0) 收藏 举报
分类:
xmpp(32)
http://iammr.7.blog.163.com/blog/static/49102699201041961613109/
想象中如此简单的功能,想不到却这般大费周折。
如要实现,必须先确保:
1. openfire中安装有“Presence” 插件。
2. 确保该插件设置可允许任何人访问(如果是跨域浏览的话)
然后通过如下方式访问:http://www.igniterealtime.org/projects/openfire/plugins/presence/readme.html。
访问结果如下:
| 账号 | 状态 | xml | text |
| user1 | 不存在 | <presence type="error" from="[email protected]"><error code="403" type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence> | null |
| user2 | 离线 | <presence type="unavailable" from="[email protected]"><status>Unavailable</status></presence> | Unavailable |
| user8 | 在线 | <presence from="[email protected]/trm"><priority>0</priority></presence> 或者 <presence id="6Mgiu-13" from="[email protected]/Smack"/> |
null |
java代码:
import java.net.*;
import java.io.*;
/**
* 判断openfire用户的状态
* strUrl : url格式 - http://my.openfire.com:9090/plugins/presence/[email protected]&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;
}
转载于:https://my.oschina.net/iWage/blog/537876