最近在看ios xmpp Framework,对着文档整理了一部分,不喜勿喷.
介绍
说明:这部分主要是从项目的机构
框架主要分成两部分
- The xmpp core
- 扩展部分
- rostor
- XEP’s
- Optinal suppoting utilities
- Etc
XMPPCORE 实现说明
The xmppcore is the implementation of the xmpp specification (RFC 3920).
不要搞错了xmpp的和聊天的定义
xmpp 是可扩展的消息和存在的协议,它主要是用于生成协议并且存在很多的扩展可以用于不同的目的.
扩展中包含rostoer suppet,自动重连、等各种各样的xep协议
具体XEP协议请参考(XEP's)
XMPP Core
项目Core包下的文件包含
- XMPPStream
这是一个很重要的类,所有的扩展都会收到它的影响,它具有灵活,可扩展,并且易于开发的特点.
- XMPPParser
- 一个内部类,了解一下就行吧
- XMPPJID
- 每条消息对应一个不变的唯一标识,在各种各样的消息中提取出JID.
- It conforms to the NSCopyingprotocol so that JID's may be used as keys in NSDictionary. It even conforms tothe NSCoding protocol.
- XMPPElement
XMPPElement继承与NSXMLElement并且是XMPPIQ, XMPPMessage &XMPPPresence的父类.
- XMPPIQ
- XMPPMessage
- XMPPPresence
- Elements: IQ, Message, &Presence
- XMPPModule
为扩展提供基础的类,是在开发过程中很有可能创建自定义类注册在此基础之上,但是,如果你实现一个标准的XEP,或者你想你的程序可以搞一些特殊的扩展,你必须在XMPPModule的基础之上构建.
- XMPPLogging
提供一个强大、灵活、非常快的日志框架
- XMPPInterna
内部类
元素IQ, Message, & Presence
XMPPIQ -> XMPPElement ->NSXMLElement -> NSXMLNode -> NSObject
XMPPMessage -> XMPPElement-> NSXMLElement -> NSXMLNode -> NSObject
XMPPPresence -> XMPPElement-> NSXMLElement -> NSXMLNode -> NSObject
NSXMLElement+XMPPcategory 在NSXML的基础上提价了一些方法使得代码更加简明、可读.
Forexample:
[element attributeIntValueForName:@"age"];
关于元素的更多信息请查看 Working With Elements
XMPPStreamConfiguration
XMPPStream可的配置分为以下的几个部分
配置怎么链接XMPP SERVER
添加delegates
添加modules
链接
用户身份验证
链接设置
只需要为xmppStream设置myJID
xmppStream.myJID =[XMPPJIDjidWithString:@"user@gmail.com"];
gmail.com 这个是服务器的地址
如果你知道需要链接主机的hostName,这样设置就行了
xmppStream.myJID =[XMPPJIDjidWithString:@"user@myCompany.com"];
xmppStream.hostName = @"myCompany.com";
如果未设置xmppStream.hostName则会通过SRV 查找
同样也可以掉过DNS解析,直接设置服务器地址
xmppStream.myJID =[XMPPJIDjidWithString:@"user@dev1.myCompany.com"];
xmppStream.hostName = @"192.168.2.27";
还有一个可选项目为hostPort 根据每个服务器的定义,通常情况下会运行在5222端口上.如果你的服务上有不同的port,你需要设置hostPort属性
添加Delegates
多路广播MulticastDelegate是其中一个很有特色的亮点
xmpp framework需要支持很多扩展,其中包含光放的扩展、自定义扩展.这样的话传统的delegate模式不能满足需求了,框架需要分开每种广播并且保证每个类都收到delegate的回调方法.And the standard NSNotification architecture won't work either becausesome of these delegates require a return variable. (Plus it's really annoyingto extract parameters from a notification's userInfo dictionary.)
MulticastDelegate准许你在框架中使用标准的delegate规范,它可以像delegate消息一样使多个类收到消息.The beauty of this is that youdon't have to put all your xmpp handling code in a single class. You canseparate your handling into multiple classes, or however you see fit
可以任何时候添加或移除自己的delegate
[xmppStream addDelegate:selfdelegateQueue:dispatch_get_main_queue()];
[xmppStream removeDelegate:self];
添加模块
它可以运送大量的扩展,同样也可以添加自己需要的扩展.不需要关注所有已经可以的扩展.
- XMPPReconnect – 如果失去连接,自动重连
- XMPPRoster – 提供标准的xmpp roster支持
- XMPPRoom – 提供多用户聊天支持
XMPPPubSub – 发布订阅
Forexample
我们将为我们的stream插入XMPPReconnect module
xmppReconnect =[[XMPPReconnectalloc] init];
// Optional configuration of xmppReconnectcould go here.
// The defaults are fine for our purposes.
[xmppReconnect activate:xmppStream];
// You can also optionally add delegates tothe module.
[xmppReconnect addDelegate:selfdelegateQueue:dispatch_get_main_queue()];
// And that's all that is needed.
// The module will receive any delegatemethods it needs automatically
// from the xmpp stream, and willcontinue to do its thing unless you deactivate it.
如果已经准备好了,可以开始链接过程了
NSError *error = nil;
if(![xmppStream connect:&error])
{
NSLog(@"Oops,I probably forgot something: %@", error);
}
如果你忘记必须的选项,比如说myJID,connect方法将会返回NO,errormessage信息将要输出错误信息
在链接过程中,客户端和服务器端通过xmpp握手.这个时候,服务器端告诉客户端支持的各种协议。一些服务器也许需要链接过程加密.如果是这个原因,xmpp stream将自动进行安全连接.如果使用错误的证书,你需要实现xmppStream:willSecureWithSettings:delegate方法使用默认的安全设置
认证(Authenticating)
当所有的链接握手完成即xmppStreamDidConnect: delegate method被回调.大多是客户端开始进行用户身份验证:
-(void)xmppStreamDidConnect:(XMPPStream *)sender
{
[xmppStream authenticateWithPassword:password error:NULL];
}
XMPP Logging
框架中log的几个目标
- 必须支持几个log级别
- 必须提供一个可配置的基础文件
- 必须被用户配置
https://github.com/robbiehanson/XMPPFramework/wiki/IntroToFramework
转载于:https://blog.51cto.com/scxixi/1052363