2013-10-22html
一个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的绑定过程。node
客户端经过服务端的验证以后,应该给XMPP流绑定一个特殊的资源以使服务端可以正确的定位到客户端。客户端Bare JID后必须附带resource,与服务端交互时使用Full JID,这样就确保服务端和客户端传输XML段时服务端可以正确的找到对应的客户端。web
当一个客户端经过一个资源绑定到XML流上后,它就被称之为"已链接的资源"。服务器应该容许同时处理多个”已链接的资源“,每一个”已链接的资源“由不一样的XML流合不一样的resource来区分。服务器
资源绑定用到的XML命名空间为 "urn:ietf:params:xml:ns:xmpp-bind" .session
服务端在SASL协议成功,发送了响应的stream头以后,必需紧接着发送一个由'urn:ietf:params:xml:ns:xmpp-bind'
标识的<bind/>
元素。dom
S:<stream:stream
from='im.example.com'
id='gPybzaOzBmaADgxKXu9UClbprp0='
to='juliet@im.example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
S:<stream:features>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
</stream:features>
一个资源标识至少在同一个Bare JID的全部resource标识中是惟一的,这一点须要由服务端来负责。this
客户端经过发送一个包含空的<bind/>
元素,类型为的set
的IQ
来请求服务端生成resource标志。spa
C:<iq id='tn281v37' type='set'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
</iq>
服务端生成后发送响应给客户端:code
S:<iq id='tn281v37' type='result'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
<jid>
juliet@im.example.com/4db06f06-1ea4-11dc-aca3-000bcd821bfb
</jid>
</bind>
</iq>
失败状况:orm
客户端也能够本身设置resource。
客户端经过发送一个包含<bind/>
元素,类型为的set
的IQ
来请求服务端生成resource标志。<bind/>
元素包含一个子元素<resource/>
。<resource/>
元素包含长度非零的字符串。
C:<iq id='wy2xa82b4' type='set'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
<resource>balcony</resource>
</bind>
</iq>
服务端应该接受客户端提交的resource标志。服务端经过IQ
返回一个<bind/>
元素,其中包含了一个<jid>
元素,<jid>
元素中包含一个Full JID,其中的resource是客户端提交的resource标志。
S:<iq id='wy2xa82b4' type='result'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
<jid>juliet@im.example.com/balcony</jid>
</bind>
</iq>
服务端有可能会拒绝客户端提供的resource标志,而使用服务端生成的resource标志。
失败状况:
当客户端提供的resource标志冲突时,服务端应该遵循如下三个策略之一:
若是是第一种状况,服务端返回从新生成的resource标志:
S:<iq id='wy2xa82b4' type='result'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
<jid>
juliet@im.example.com/balcony 4db06f06-1ea4-11dc-aca3-000bcd821bfb
</jid>
</bind>
</iq>
若是是第二种状况,服务端向新链接返回一个<conflict/>
流错误:
S:<iq id='wy2xa82b4' type='error'>
<error type='modify'>
<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</iq>
若是是第三种状况,服务端向已链接的客户端发送<conflict/>
流错误,关闭已链接的客户端的流,而后向新的链接发送绑定的结果:
S:<iq id='wy2xa82b4' type='result'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
<jid>
juliet@im.example.com/balcony
</jid>
</bind>
</iq>
相似QQ,不一样设备上最多只能有一个帐户在线,例如一个帐号不能在两个iPhone上同时在线,在第二台上登陆,就要把第一台踢下线,可是,又容许桌面或web上登陆相同的帐号。像这样的需求,能够经过resource标志来实现。
假定策略以下:一个帐号最多只能在相同系统的设备上有一处登陆,例如,用户在一台iPhone上登陆,若是又在另一台设备上登陆,那就把第一台踢下线,可是能够容许有一个Android设备登陆相同的帐号。
实现: 在iOS版本中,登陆时,客户端提交自定义的resource标志: iOS,一样,Android版本中,提交自定义的resource标志: Android。这样就能够限制相同系统只能有一处登陆了。
假如要求一个帐号只能在一个移动设备上登陆,实现的时候,则须要iOS和Android使用相同的resource标志,例如: Mobile.
须要特别说明的是,当旧的链接被踢下线后,服务端向客户端发送<conflict/>
流错误,并关闭流。客户端须要正确的处理这种状况下的应用逻辑。
在管理器的的 服务器>服务器管理器>系统属性 中设置属性xmpp.session.conflict-limit
的值。
Openfire相关的源码以下,能够根据须要配置对应的属性值:
String username = authToken.getUsername().toLowerCase();
// If a session already exists with the requested JID, then check to see
// if we should kick it off or refuse the new connection
ClientSession oldSession = routingTable.getClientRoute(new JID(username, serverName, resource,true));
if(oldSession !=null){
try{
int conflictLimit = sessionManager.getConflictKickLimit();
if(conflictLimit ==SessionManager.NEVER_KICK){
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(PacketError.Condition.conflict);
// Send the error directly since a route does not exist at this point.
session.process(reply);
returnnull;
}
int conflictCount = oldSession.incrementConflictCount();
if(conflictCount > conflictLimit){
// Kick out the old connection that is conflicting with the new one
StreamError error =newStreamError(StreamError.Condition.conflict);
oldSession.deliverRawText(error.toXML());
oldSession.close();
}
else{
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(PacketError.Condition.conflict);
// Send the error directly since a route does not exist at this point.
session.process(reply);
returnnull;
}
}
catch(Exception e){
Log.error("Error during login", e);
}
}
ps: 修改完须要重启openfire。
设置XMPPStream
类的实例变量myJID
,附带自定义的resource便可。
XMPPJID *myJID =[XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@/%@",user,XMPP_SERVER_ACCOUNT_HOSTNAME,@"xmpp"]];
[self.xmppStream setMyJID:myJID];
在XMPPStreamDelegate
的回调方法中处理资源绑定冲突产生的流错误:
-(void)xmppStream:(XMPPStream*)sender didReceiveError:(id)error
{
NSXMLElement*element =(NSXMLElement*) error;
NSString*elementName =[element name];
//<stream:error xmlns:stream="http://etherx.jabber.org/streams">
// <conflict xmlns="urn:ietf:params:xml:ns:xmpp-streams"/>
//</stream:error>
if([elementName isEqualToString:@"stream:error"]||[elementName isEqualToString:@"error"])
{
NSXMLElement*conflict =[element elementForName:@"conflict" xmlns:@"urn:ietf:params:xml:ns:xmpp-streams"];
if(conflict)
{
}
}
}