XMPP登陆互踢

10 篇文章 0 订阅
1 篇文章 0 订阅
如果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一致的情况有两种处理的可能:

[quote]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. [/quote]

[color=red]简单的说也就是如果resource一致,第一个办法是终结活动着的那个resource,然后让最新请求的会话留下来,也就是A先登陆,B再登陆,A会掉线。第二个办法是拒绝最新的请求会话,保持第一个,也就是A先登陆,B再登陆,B登不上去。[/color]

我的实例中的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];
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值