在XMPP的《XMPP框架_协议》 文章中介绍了很多XMPP的相关协议的内容,这些协议的实现都是基于IQ的。
IQ(info/query)是XMPP定义的的一种用来查询,发送请求等操作的指令工具集。
它为XMPP通讯提供了请求和响应机制,类似于http请求的GET/POST/PUT方法,有四种类型:
我们来看一下iOS的XMPPFramework里面的XMPPIQ类,它是如何对IQ进行封装的。
1、首先,作者说明了这个类的编写意图:
/**
* The XMPPIQ class represents an <iq> element.
* It extends XMPPElement, which in turn extends NSXMLElement.
* All <iq> elements that go in and out of the
* xmpp stream will automatically be converted to XMPPIQ objects.
*
* This class exists to provide developers an easy way to add functionality to IQ processing.
* Simply add your own category to XMPPIQ to extend it with your own custom methods.
**/
XMPPIQ这个类代表了一个<iq>元素,它是XMPPElement的扩展。
它方便了开发者更好的处理IQ,同时方便开发者在此类基础上进行扩展自定义开发。
2、便利构造方法
/**
* Converts an NSXMLElement to an XMPPIQ element in place (no memory allocations or copying)
**/
+ (XMPPIQ *)iqFromElement:(NSXMLElement *)element;
/**
* Creates and returns a new autoreleased XMPPIQ element.
* If the type or elementID parameters are nil, those attributes will not be added.
**/
+ (XMPPIQ *)iq;
+ (XMPPIQ *)iqWithType:(nullable NSString *)type;
+ (XMPPIQ *)iqWithType:(nullable NSString *)type to:(nullable XMPPJID *)jid;
+ (XMPPIQ *)iqWithType:(nullable NSString *)type to:(nullable XMPPJID *)jid elementID:(nullable NSString *)eid;
+ (XMPPIQ *)iqWithType:(nullable NSString *)type to:(nullable XMPPJID *)jid elementID:(nullable NSString *)eid child:(nullable NSXMLElement *)childElement;
+ (XMPPIQ *)iqWithType:(nullable NSString *)type elementID:(nullable NSString *)eid;
+ (XMPPIQ *)iqWithType:(nullable NSString *)type elementID:(nullable NSString *)eid child:(nullable NSXMLElement *)childElement;
+ (XMPPIQ *)iqWithType:(nullable NSString *)type child:(nullable NSXMLElement *)childElement;
便利构造方法
iqFromElement 方法是从一个XMPPElement来构造出一个XMPPIQ对象
这里用到runtime方法
object_setClass(element, [XMPPIQ class]);
直接把element对象设置成XMPPIQ类。
后面几个方法通过源码我们知道都是一层层往下调用,最终调用的都是调用到这个核心的方法:
- (id)initWithType:(NSString *)type to:(XMPPJID *)jid elementID:(NSString *)eid child:(NSXMLElement *)childElement
{
if ((self = [super initWithName:@"iq"]))
{
if (type)
[self addAttributeWithName:@"type" stringValue:type];
if (jid)
[self addAttributeWithName:@"to" stringValue:[jid full]];
if (eid)
[self addAttributeWithName:@"id" stringValue:eid];
else
[self addAttributeWithName:@"id" stringValue:[[NSUUID UUID] UUIDString]];
if (childElement)
[self addChild:childElement];
}
return self;
}
type:类型,前面介绍过的IQ类型
to:IQ查询的接收方,通常指服务器
id:elementId 如果不指定就默认通过[NSUUID UUID]方法来生成,
childElement:子元素,如果有就调用 [self addChild]方法进行添加。
这些属性如果有就通过addAttributWithName方法添加进去,子元素也一样如果有就通过调用addChild方法添加进去。
3、便利属性
/**
* Returns the type attribute of the IQ.
* According to the XMPP protocol, the type should be one of 'get', 'set', 'result' or 'error'.
*
* This method converts the attribute to lowercase so
* case-sensitive string comparisons are safe (regardless of server treatment).
**/
@property (nonatomic, readonly, nullable) NSString *type;
/**
* Convenience methods for determining the IQ type.
**/
@property (nonatomic, readonly) BOOL isGetIQ;
@property (nonatomic, readonly) BOOL isSetIQ;
@property (nonatomic, readonly) BOOL isResultIQ;
@property (nonatomic, readonly) BOOL isErrorIQ;
/**
* Convenience method for determining if the IQ is of type 'get' or 'set'.
**/
@property (nonatomic, readonly) BOOL requiresResponse;
判断IQ类型,需要查询返回等。
4、NSXMLElement相关的
/**
* The XMPP RFC has various rules for the number of child elements an IQ is allowed to have:
*
* - An IQ stanza of type "get" or "set" MUST contain one and only one child element.
* - An IQ stanza of type "result" MUST include zero or one child elements.
* - An IQ stanza of type "error" SHOULD include the child element contained in the
* associated "get" or "set" and MUST include an <error/> child.
*
* The childElement returns the single non-error element, if one exists, or nil.
* The childErrorElement returns the error element, if one exists, or nil.
**/
@property (nonatomic, readonly, nullable) NSXMLElement *childElement;
@property (nonatomic, readonly, nullable) NSXMLElement *childErrorElement;
XMPPIQ继承于XMPPElement,同时它又是NSXMLElement的子类。
childElement用来获取子元素,childErrorElement用来获取一个错误的子元素。