iOS 使用xmpp做聊天客户端

可以号称史上最详细的xmpp做iOS客户端聊天介绍。

简介:XMPP协议是一种基于Socket长连接、以XML格式进行基本信息交换、C/S  S/S多种架构的聊天协议

XMPPServer 基于XMPP协议的服务端(例如eJabber、OpenFire)

openfire服务器安装和配置连接地址: http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html

 

一.框架导入

 

1.在header search Path 中 添加 /user/include/libxml2

2.添加libxml2.dylib与libresolv.dylib

3.拷贝源码目录下的 Authentication Categories Core 和 Utilities 到项目根目录下并添加到项目中

 

二.核心库

 

 

1.XMPPStream   核心中的核心:xml流

2.XMPPElement   xml基本元素/stanza

3.XMPPIQ  查询节点 Info/Query的缩写 类似于HTTP请求

4.XMPPJID  用户的标识/身份证

5.XMPPMessage   基本消息节点(XML)

6. XMPPPresence  出席节点(XML

7.XMPPParser   XML解析,Core中唯一一个不太重要的类

8.XMPPModule  各种功能模块的基类,继承它的模块均需在xmppStream中激活,基于多播代理,可添加多个委托实例

 

 

 

 

 

三.扩展库

 

1.XMPPRoster好友列表

2.XMPPReconnect 重连

3.ProcessOne 推送通知与快速重连

4.XMPPBandwidthMonitor   带宽监视

5.XMPPFileTransfer 文件传输

6.XMPPRoom(XEP-0045) 聊天室

7.XMPPvCard(XEP--0054) 个人资料/名片

8.XMPPResultSet(XEP-0059) XML中的结果集

9.XMPPPubSub(XEP-0060)  发布/订阅模块

10.XMPPRegistration(XEP-0077) 注册与密码修改

11.XMPPMessage+XEP_0085 消息节点的聊天状态扩展

12.XMPPMessageArchiving(XEP-0136) 聊天记录同步

13.XMPPMessageDeliveryReceipts(XEP-0184) 接受消息回执

14.XMPPBlocking(XEP-0191) 黑名单/屏蔽用户

15.XMPPStreamManagement(XEP-0198) XML流恢复(区别于Reconnect)

16.XMPPAutoPing(XEP-0199) 心跳检测

17.XMPPAutoTime(XEP-0202) 时间比对

18.NSXMLElement+XEP_0203 (DelayedMessage)延迟消息

19.XMPPAttentionModule(XEP-0224) 引起对方注意的消息模块,需激活

20.XMPPMessageCarbons(XEP-0280) 同一个用户多设备登陆(jid的资源部分不同)时的消息多发

21.NSXMLElement+XEP_0297   XML节点扩展--消息转发

22.XMPPMessage+XEP_0308 一种特殊消息:对已经发送的某条消息进行更改/订正

23.XMPPMessage+XEP_0333 更佳先进的消息回执  Message的分类

24.XMPPElement+JSON(XEP-0335) 在XML节点中插入JSON

 

四.xmpp建立连接并登录

 

1.新建一个 XMPPStream 对象,添加委托

添加委托方法 - (void)addDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue

参数 delegateQueue 为委托回调所使用的 GCD 队列,dispatch_get_main_queue() 获取主线程 GCD 队列

 

2.xmppjid

JID 一般由三部分构成:用户名,域名和资源名,例如 test@example.com/Anthony  如果没有设置主机名,则使用 JID 的域名作为主机名  端口号是可选的,默认是 5222

 

3.身份认证

实现 - (void)xmppStreamDidConnect:(XMPPStream *)sender 委托方法

连接服务器成功后,回调该方法

身份认证方法 - (BOOL)authenticateWithPassword:(NSString *)inPassword error:(NSError **)errPtr

 

4.上线

实现 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender 委托方法 身份认证成功后,回调该方法 新建一个 XMPPPresence 对象,类型为 available,发送!

 

5.退出并断开连接

新建一个 XMPPPresence 对象,类型为 unavailable,发送!

断开连接

 

五.xmpp注册

 

1.判断xmpp否连接,是否带注册支持

[[self appDelegate] xmppStream] isConnected] && [[[self appDelegate]xmppStream] supportsInBandRegistration]

 

2.开始注册

设置myjid和密码 1.setMyJID  2.registerWithPassword

 

六.好友列表

 

  1. 获取好友列表 

   注意本地数据库缓存   NSManagedObjectContext *context = [[[self appDelegate] xmppRosterStorage] mainThreadManagedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" inManagedObjectContext:context];    NSFetchRequest *request = [[NSFetchRequest alloc]init];

[request setEntity:entity];

    NSError *error ;

 NSArray *friends = [context executeFetchRequest:request error:&error];

 

2.对方添加好友时 更新列表 // 已经互为好友以后,会回调此  

- (void)xmppRoster:(XMPPRoster *)sender didReceiveRosterItem:(NSXMLElement *)item {  

NSString *subscription = [item attributeStringValueForName:@"subscription"];  

 if ([subscription isEqualToString:@"both"]) {  

NSLog(@"双方已经互为好友");  

if (self.buddyListBlock) {  

// 更新好友列表  

 }  

}  

}

 

七.xmpp添加好友

  [[[self appDelegate] xmppRoster] addUser:[XMPPJID jidWithString:@"admin@127.0.0.1"] withNickname:@"admin"];

 

八.xmpp发送消息和接收消息

 

1.发送消息

我们需要根据 XMPP 协议,将数据放到 <message /> 标签内,例如:

<message type="chat" to="xiaoming@example.com">

<body>Hello World!<body />

<message />

- (void)sendMessage:(NSString *) message toUser:(NSString *) user {

    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];

    [body setStringValue:message];

    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];

    [message addAttributeWithName:@"type" stringValue:@"chat"];

    NSString *to = [NSString stringWithFormat:@"%@@example.com", user];

    [message addAttributeWithName:@"to" stringValue:to];

    [message addChild:body];

    [self.xmppStream sendElement:message];

}

 

2.接收消息

当接收到 <message /> 标签的内容时,XMPPFramework 框架回调该方法

根据 XMPP 协议,消息体的内容存储在标签 <body /> 内

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {

    NSString *messageBody = [[message elementForName:@"body"] stringValue];

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 




原文地址:https://www.cnblogs.com/xmandxx/p/4587915.html