原文:http://blog.csdn.net/brasbug/article/details/26353511

一个XMPP的账号由三部分组成: 用户名(user/node),域名(domain)和资源(resource) 。例如 alice@xmpp.irusher.com/mobile ,user部分(或node)是alice,domain是xmpp.irusher.com,resource部分是mobile。user和domain组合也叫Bare JID,例如:alice@xmpp.i8i8i8.com ,Bare JID常用标识一个用户。包含了user,domain和resource的ID也叫Full JID,在Full JID中,resource一般用来区分一个用户的多个会话,可以由服务端或客户端指定。下面介绍一下resource的绑定过程。

资源绑定

客户端通过服务端的验证之后,应该给XMPP流绑定一个特殊的资源以使服务端能够正确的定位到客户端。客户端Bare JID后必须附带resource,与服务端交互时使用Full JID,这样就确保服务端和客户端传输XML段时服务端能够正确的找到对应的客户端。

当一个客户端通过一个资源绑定到XML流上后,它就被称之为"已连接的资源"。服务器应该允许同时处理多个”已连接的资源“,每个”已连接的资源“由不同的XML流合不同的resource来区分。

绑定过程

1. 验证通过

服务端在SASL协议成功,发送了响应的stream头之后,必需紧接着发送一个由'urn:ietf:params:xml:ns:xmpp-bind'标识的<bind/>元素。

2. 生成resource标识

一个资源标识至少在同一个Bare JID的所有resource标识中是唯一的,这一点需要由服务端来负责。

2.1 服务端生成resource标志

客户端通过发送一个包含空的<bind/>元素,类型为的setIQ来请求服务端生成resource标志。

  1. C: <iq id='tn281v37' type='set'>
  2. <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
  3. </iq>

服务端生成后发送响应给客户端:

  1. S: <iq id='tn281v37' type='result'>
  2. <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
  3. <jid>
  4. juliet@im.example.com/4db06f06-1ea4-11dc-aca3-000bcd821bfb
  5. </jid>
  6. </bind>
  7. </iq>

失败情况:

  1. 一个Bare JID下已经达到了同时在线的上限;
  2. 客户端不被允许绑定资源

2.2 客户端设置resource标志

客户端也可以自己设置resource。

客户端通过发送一个包含<bind/>元素,类型为的setIQ来请求服务端生成resource标志。<bind/>元素包含一个子元素<resource/><resource/>元素包含长度非零的字符串。

  1. C: <iq id='wy2xa82b4' type='set'>
  2. <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
  3. <resource>balcony</resource>
  4. </bind>
  5. </iq>

服务端应该接受客户端提交的resource标志。服务端通过IQ返回一个<bind/>元素,其中包含了一个<jid>元素,<jid>元素中包含一个Full JID,其中的resource是客户端提交的resource标志。

  1. S: <iq id='wy2xa82b4' type='result'>
  2. <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
  3. <jid>juliet@im.example.com/balcony</jid>
  4. </bind>
  5. </iq>

服务端有可能会拒绝客户端提供的resource标志,而使用服务端生成的resource标志。

失败情况:

  1. 提交的标志包含非法字符,地址格式可以在这里查到: Address Format
  2. 提交的标志已经被占用

2.2.1 resource标志冲突

当客户端提供的resource标志冲突时,服务端应该遵循以下三个策略之一:

  1. 重新生成新连接提交的resource标志,使新连接能够继续;
  2. 拒绝新的连接,并维持现有的连接;
  3. 断开现有的连接,并尝试绑定新的连接;

如果是第一种情况,服务端返回重新生成的resource标志:

  1. S: <iq id='wy2xa82b4' type='result'>
  2. <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
  3. <jid>
  4. juliet@im.example.com/balcony 4db06f06-1ea4-11dc-aca3-000bcd821bfb
  5. </jid>
  6. </bind>
  7. </iq>

如果是第二种情况,服务端向新连接返回一个<conflict/>流错误:

  1. S: <iq id='wy2xa82b4' type='error'>
  2. <error type='modify'>
  3. <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  4. </error>
  5. </iq>

如果是第三种情况,服务端向已连接的客户端发送<conflict/>流错误,关闭已连接的客户端的流,然后向新的连接发送绑定的结果:

  1. S: <iq id='wy2xa82b4' type='result'>
  2. <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
  3. <jid>
  4. juliet@im.example.com/balcony
  5. </jid>
  6. </bind>
  7. </iq>

应用:强制下线

类似QQ,不同设备上最多只能有一个账户在线,例如一个账号不能在两个iPhone上同时在线,在第二台上登录,就要把第一台踢下线,但是,又允许桌面或web上登录相同的账号。像这样的需求,可以通过resource标志来实现。

假定策略如下:一个账号最多只能在相同系统的设备上有一处登录,例如,用户在一台iPhone上登录,如果又在另外一台设备上登录,那就把第一台踢下线,但是可以允许有一个Android设备登录相同的账号。

实现: 在iOS版本中,登录时,客户端提交自定义的resource标志: iOS,同样,Android版本中,提交自定义的resource标志: Android。这样就可以限制相同系统只能有一处登录了。

假如要求一个账号只能在一个移动设备上登录,实现的时候,则需要iOS和Android使用相同的resource标志,例如: Mobile.

需要特别说明的是,当旧的连接被踢下线后,服务端向客户端发送<conflict/>流错误,并关闭流。客户端需要正确的处理这种情况下的应用逻辑。

Openfire配置冲突解决策略

在管理器的的 服务器>服务器管理器>系统属性 中设置属性xmpp.session.conflict-limit的值。

Openfire相关的源码如下,可以根据需要配置对应的属性值:

  1. String username = authToken.getUsername().toLowerCase();
  2. // If a session already exists with the requested JID, then check to see
  3. // if we should kick it off or refuse the new connection
  4. ClientSession oldSession = routingTable.getClientRoute(new JID(username, serverName, resource, true));
  5. if (oldSession != null) {
  6. try {
  7. int conflictLimit = sessionManager.getConflictKickLimit();
  8. if (conflictLimit == SessionManager.NEVER_KICK) {
  9. reply.setChildElement(packet.getChildElement().createCopy());
  10. reply.setError(PacketError.Condition.conflict);
  11. // Send the error directly since a route does not exist at this point.
  12. session.process(reply);
  13. return null;
  14. }
  15. int conflictCount = oldSession.incrementConflictCount();
  16. if (conflictCount > conflictLimit) {
  17. // Kick out the old connection that is conflicting with the new one
  18. StreamError error = new StreamError(StreamError.Condition.conflict);
  19. oldSession.deliverRawText(error.toXML());
  20. oldSession.close();
  21. }
  22. else {
  23. reply.setChildElement(packet.getChildElement().createCopy());
  24. reply.setError(PacketError.Condition.conflict);
  25. // Send the error directly since a route does not exist at this point.
  26. session.process(reply);
  27. return null;
  28. }
  29. }
  30. catch (Exception e) {
  31. Log.error("Error during login", e);
  32. }
  33. }
  1. xmpp.session.conflict-limit == -1 :向新连接发送资源绑定冲突的流错误;
  2. xmpp.session.conflict-limit <= 1 && != -1 : 向旧连接发送资源绑定冲突的流错误,并且关闭旧的连接(会话);
  3. xmpp.session.conflict-limit > 1: 向新连接发送资源绑定冲突的流错误;

ps: 修改完需要重启openfire。

XMPPFramework处理

自定义resource

设置XMPPStream类的实例变量myJID,附带自定义的resource即可。

  1. XMPPJID *myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@/%@",user,XMPP_SERVER_ACCOUNT_HOSTNAME,@"xmpp"]];
  2. [self.xmppStream setMyJID:myJID];

处理冲突

XMPPStreamDelegate的回调方法中处理资源绑定冲突产生的流错误:

  1. - (void)xmppStream:(XMPPStream *)sender didReceiveError:(id)error
  2. {
  3. NSXMLElement *element = (NSXMLElement*) error;
  4. NSString *elementName = [element name];
  5. //<stream:error xmlns:stream="http://etherx.jabber.org/streams">
  6. // <conflict xmlns="urn:ietf:params:xml:ns:xmpp-streams"/>
  7. //</stream:error>
  8. if ([elementName isEqualToString:@"stream:error"] || [elementName isEqualToString:@"error"])
  9. {
  10. NSXMLElement *conflict = [element elementForName:@"conflict" xmlns:@"urn:ietf:params:xml:ns:xmpp-streams"];
  11. if (conflict)
  12. {
  13. }
  14. }
  15. }

XMPP资源绑定(Resource Binding)与单台设备登录控制的更多相关文章

  1. XMPP资源绑定(Resource Binding)

    一个XMPP的账号由三部分组成: 用户名(user/node),域名(domain)和资源(resource) .例如 alice@xmpp.irusher.com/mobile ,user部分(或n ...

  2. DNS资源纪录(Resource Record)介绍

          http://dns-learning.twnic.net.tw/bind/intro6.html 类型 SOA NS A AAAA PTR CNAME MX -------------- ...

  3. php的redis 操作类,适用于单台或多台、多组redis服务器操作

    redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...

  4. 高性能网络编程(一):单台服务器并发TCP连接数到底可以有多少

    高性能网络编程(一):单台服务器并发TCP连接数到底可以有多少     阅读(81374) | 评论(9)收藏16 淘帖1 赞3   JackJiang Lv.9    1 年前 | 前言 曾几何时我 ...

  5. spring mvc: 资源绑定视图解析器(不推荐)

    spring mvc: 资源绑定视图解析器(不推荐) 不适合单控制器多方法访问,有知道的兄弟能否告知. 访问地址: http://localhost:8080/guga2/hello/index 项目 ...

  6. 网络编程释疑之:单台服务器上的并发TCP连接数可以有多少

    曾几何时我们还在寻求网络编程中C10K问题的解决方案,但是现在从硬件和操作系统支持来看单台服务器支持上万并发连接已经没有多少挑战性了.我们先假设单台服务器最多只能支持万级并发连接,其实对绝大多数应用来 ...

  7. Spring MVC资源绑定视图解析器

    ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称. 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewRes ...

  8. 读懂在单台机器上创建RabbitMQ集群

    在优锐课java中了解有关在单台计算机上安装集群以及如何向集群添加更多节点的更多信息,码了很多专业的相关知识, 分享给大家参考学习. 如果你在单台计算机上设置群集时遇到问题,那么以下文章可能会帮助回答 ...

  9. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

随机推荐

  1. python logging system

    官方教程:https://docs.python.org/2/library/logging.html 1.  用法1 import logging import logging.handlers L ...

  2. 简单搞懂OAuth2.0

    本文转自:https://www.cnblogs.com/flashsun/p/7424071.html 原作者:闪客sun 一张图搞定OAuth2.0 目录 1.引言 2.OAuth2.0是什么 3 ...

  3. linux命令(25):ln命令

    命令格式: ln [参数][源文件或目录][目标文件或目录] 必要参数: -b 删除,覆盖以前建立的链接 -d 允许超级用户制作目录的硬链接 -f 强制执行 -i 交互模式,文件存在则提示用户是否覆盖 ...

  4. LeetCode214. Shortest Palindrome

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. hdu 1428(很好的一道题,最短路+记忆化搜索)

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. AC日记——[ZJOI2012]网络 bzoj 2816

    2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...

  7. manacher算法求最长回文子串

    一:背景 给定一个字符串,求出其最长回文子串.例如: s="abcd",最长回文长度为 1: s="ababa",最长回文长度为 5: s="abcc ...

  8. Python WindowsError

    WindowsError: [Error 2] The system cannot find the file specified WindowsError: [Error 3] The system ...

  9. PAT L3-002. 堆栈

    树状数组,二分. 一堆数字,可以删除栈顶,压入数字,求中位数,可以线段树,也可以树状数组上二分. #include<map> #include<set> #include< ...

  10. Protocol Buffers 在前端项目中的使用

    前言: 公司后端使用的是go语言,想尝试用pb和前端进行交互,于是便有了这一次尝试,共计花了一星期时间,网上能查到的文档几乎都看了一遍,但大多都是教在node环境下如何使用,普通的js环境下很多讲述的 ...