如果JID的resource缺省,则登陆后XMPP服务器会自动分配一个resource,如tigase-10.由于XMPP是支持多点登陆的,所以不同客户端用同一个JID登陆后,服务器会为每一个客户端分配一个不同的resource以区分他们。比如A客户端的resource是tigase-10,B客户端是tigase-11,这时A的客户端的完整jid是borissun@iteye.com/tigase-10,B客户端的jid是borissun@iteye.com/tigase-11。这样如果C客户端向borissun@iteye.com/tigase-10发消息,则A会收到,向borissun@iteye.com/tigase-11则B会收到,但是向borissun@iteye.com发消息,服务器不知道到底是给谁发了,因此一定有一个客户端会丢失消息。
如果想将XMPP做成只有一个客户端登陆,则jid的resource就要一致,比如borissun@iteye.com/boris .XMPP 的server对于resource一致的情况有两种处理的可能:
引用
If there is already an active resource of the same name, the server MUST either (1) terminate the active resource and allow the newly-requested session, or (2) disallow the newly-requested session and maintain the active resource. Which of these the server does is up to the implementation, although it is RECOMMENDED to implement case #1. In case #1, the server SHOULD send a <conflict/> stream error to the active resource, terminate the XML stream and underlying TCP connection for the active resource, and return a IQ stanza of type "result" (indicating success) to the newly-requested session. In case #2, the server SHOULD send a <conflict/> stanza error to the newly-requested session but maintain the XML stream for that connection so that the newly-requested session has an opportunity to negotiate a non-conflicting resource identifier before sending another request for session establishment.
简单的说也就是如果resource一致,第一个办法是终结活动着的那个resource,然后让最新请求的会话留下来,也就是A先登陆,B再登陆,A会掉线。第二个办法是拒绝最新的请求会话,保持第一个,也就是A先登陆,B再登陆,B登不上去。
我的实例中的server是采用的第一种方法,不管哪一种方法,要掉线的客户端会收到server发的一个error:
<stream:error xmlns:stream="http://etherx.jabber.org/streams">
<conflict xmlns="urn:ietf:params:xml:ns:xmpp-streams"></conflict>
</stream:error>
conflict代表了登陆冲突,因此只要收到了这个消息,就可以确定有其他客户端登陆了同样的jid,因此我们只用处理这个error就好。
在OC的XMPPFramework中可以这样处理:
- (void)xmppStream:(XMPPStream *)sender didReceiveError:(id)error
{
HWLOGI(@"didReceiveError:%@",error);
DDXMLNode *errorNode = (DDXMLNode *)error;
//遍历错误节点
for(DDXMLNode *node in [errorNode children])
{
//若错误节点有【冲突】
if([[node name] isEqualToString:@"conflict"])
{
//停止轮训检查链接状态
[_timer invalidate];
NSString *message = [NSString stringWithFormat:@"%@さんが別の端末でログインしたため、自動的にログアウトしました。",[[[SharedAppDelegate myInfo] myCardDetial]fullNameWithSpace]];
//弹出登陆冲突,点击OK后logout
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
alert.tag = 9999;
alert.delegate = SharedAppDelegate;
[alert show];
[alert release];
}
}
}
分享到:
相关推荐
XMPP 登陆demo 所使用的XMPP框架是3.6.4
微信聊天界面,登陆采用XMPP实现,本地服务器
XMPP体系架构 XMPP server:其内核是一个XMPP路由器,完成基本组件间的数据包交换和路由。 功能: 1.会话管理器:负责客户端会话认证,在线状态,用户联系表等 2.数据存储器(XDB):连接数据库系统,保持用户信息、...
xmpp协议介绍PPT,详细介绍了XMPP核心协议方方面面
android的XMPP客户端
通过XMPP请求服务器,完成注册和登陆。注册和登陆与服务器有协议,当请求网络时需要设置传递参数。
xmpp协议和xmpp扩展协议,chm格式
基于XMPP的多方通信系统研究与实现.pdf 基于XMPP的P2P即时通信系统的研究及实现.kdh 基于SIMPLE和XMPP协议的移动IM研究.pdf XMPP研究与应用.pdf XMPP协议研究及其在IM系统群组通信中的应用.pdf XMPP协议分析及客户端...
XMPP是一种基于XML的协议,它继承了在XML环境中灵活的发展性。因此,基于XMPP的应用具有超强的可扩展性。经过扩展以后的XMPP可以通过发送扩展的信息来处理用户的需求,以及在XMPP的顶端建立如内容发布系统和基于地址...
XMPP协议分析,xmpp是即时通讯IM中比较普遍的应用
Learn about the fundamentals of XMPP and be able to work with the core functionality both server-side and in the browser Build a simple 1-to-1 chat (the "Hello World" of XMPP), explore multi-user chat...
erlang分布式 XMPP Seerlang分布式 XMPP Serverrvererlang分布式 XMPP Servererlang分布式 XMPP Server
关于XMPP协议的说明及介绍,XMPP体系结构的组成,XMPP原理等的说明
xmpp
XMPP的核心功能定义在Extensible Messaging and Presence Protocol (XMPP): Core [XMPP-CORE|XMPP文档列表/XMPP正式RFC标准/RFC3920]. 这些功能 -- 主要是 XML流, 使用 TLS和SASL,以及流的根元素之下的, , 和 <iq/> ...
xmpp api文档
D3Xmpp 是一个完整的 XMPP 的 Android 的项目服务端使用 openfire 3.9.3客户端使用 ...PS:该项目是本人在学习xmpp过程中总结的一些东西,代码组织凌乱,旨在帮助新入门xmpp学习xmpp的基本使用 标签:D3Xmpp
xmpp客户端源代码
ios xmpp demo