简介: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 一般由三部分构成:用户名,域名和资源名,例如 [email protected]/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:@"[email protected]"] withNickname:@"admin"];

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

1.发送消息

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

Hello World!

- (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.接收消息

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

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

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

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

}

为了方便程序调用,我们把XMPP的一些主要方法写在AppDelegate中

基于XMPP的IOS聊天客户端程序-LMLPHP

在AppDelegate.m下这几个方法为:

-(void)setupStream{

    //初始化XMPPStream
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()]; } -(void)goOnline{ //发送在线状态
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence]; } -(void)goOffline{ //发送下线状态
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[[self xmppStream] sendElement:presence]; } -(BOOL)connect{ [self setupStream]; //从本地取得用户名,密码和服务器地址
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *userId = [defaults stringForKey:USERID];
NSString *pass = [defaults stringForKey:PASS];
NSString *server = [defaults stringForKey:SERVER]; if (![xmppStream isDisconnected]) {
return YES;
} if (userId == nil || pass == nil) {
return NO;
} //设置用户
[xmppStream setMyJID:[XMPPJID jidWithString:userId]];
//设置服务器
[xmppStream setHostName:server];
//密码
password = pass; //连接服务器
NSError *error = nil;
if (![xmppStream connect:&error]) {
NSLog(@"cant connect %@", server);
return NO;
} return YES; } -(void)disconnect{ [self goOffline];
[xmppStream disconnect]; } -(void)setupStream{ //初始化XMPPStream
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()]; } -(void)goOnline{ //发送在线状态
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence]; } -(void)goOffline{ //发送下线状态
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[[self xmppStream] sendElement:presence]; } -(BOOL)connect{ [self setupStream]; //从本地取得用户名,密码和服务器地址
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *userId = [defaults stringForKey:USERID];
NSString *pass = [defaults stringForKey:PASS];
NSString *server = [defaults stringForKey:SERVER]; if (![xmppStream isDisconnected]) {
return YES;
} if (userId == nil || pass == nil) {
return NO;
} //设置用户
[xmppStream setMyJID:[XMPPJID jidWithString:userId]];
//设置服务器
[xmppStream setHostName:server];
//密码
password = pass; //连接服务器
NSError *error = nil;
if (![xmppStream connect:&error]) {
NSLog(@"cant connect %@", server);
return NO;
} return YES; } -(void)disconnect{ [self goOffline];
[xmppStream disconnect]; }这几个是基础方法,接下来就是XMPPStreamDelegate中的方法,也是接受好友状态,接受消息的重要方法 [objectc]
//连接服务器
- (void)xmppStreamDidConnect:(XMPPStream *)sender{ isOpen = YES;
NSError *error = nil;
//验证密码
[[self xmppStream] authenticateWithPassword:password error:&error]; } //验证通过
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{ [self goOnline];
} //收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{ // NSLog(@"message = %@", message); NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue]; NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:msg forKey:@"msg"];
[dict setObject:from forKey:@"sender"]; //消息委托(这个后面讲)
[messageDelegate newMessageReceived:dict]; } //收到好友状态
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence{ // NSLog(@"presence = %@", presence); //取得好友状态
NSString *presenceType = [presence type]; //online/offline
//当前用户
NSString *userId = [[sender myJID] user];
//在线用户
NSString *presenceFromUser = [[presence from] user]; if (![presenceFromUser isEqualToString:userId]) { //在线状态
if ([presenceType isEqualToString:@"available"]) { //用户列表委托(后面讲)
[chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"nqc1338a"]]; }else if ([presenceType isEqualToString:@"unavailable"]) {
//用户列表委托(后面讲)
[chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"nqc1338a"]];
} } } //连接服务器
- (void)xmppStreamDidConnect:(XMPPStream *)sender{ isOpen = YES;
NSError *error = nil;
//验证密码
[[self xmppStream] authenticateWithPassword:password error:&error]; } //验证通过
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{ [self goOnline];
} //收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{ // NSLog(@"message = %@", message); NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue]; NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:msg forKey:@"msg"];
[dict setObject:from forKey:@"sender"]; //消息委托(这个后面讲)
[messageDelegate newMessageReceived:dict]; } //收到好友状态
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence{ // NSLog(@"presence = %@", presence); //取得好友状态
NSString *presenceType = [presence type]; //online/offline
//当前用户
NSString *userId = [[sender myJID] user];
//在线用户
NSString *presenceFromUser = [[presence from] user]; if (![presenceFromUser isEqualToString:userId]) { //在线状态
if ([presenceType isEqualToString:@"available"]) { //用户列表委托(后面讲)
[chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"nqc1338a"]]; }else if ([presenceType isEqualToString:@"unavailable"]) {
//用户列表委托(后面讲)
[chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"nqc1338a"]];
} } }
这里面有两个委托方法,一个是用户列表委托,还有一个就是消息委托,用户列表委托主要就是取得在线用户,更新用户TableView,消息委托就是取得好友发送的消息,并更新消息TableView,当然这两个TableView是在不同的Controller中的 定义完两个委托,我们就要在不同的Controller中实现这两个委托了 在好友Controller中实现<KKChatDelegate>并写入如下方法 [java]
//取得当前程序的委托
-(KKAppDelegate *)appDelegate{ return (KKAppDelegate *)[[UIApplication sharedApplication] delegate]; } //取得当前的XMPPStream
-(XMPPStream *)xmppStream{ return [[self appDelegate] xmppStream];
} //在线好友
-(void)newBuddyOnline:(NSString *)buddyName{ if (![onlineUsers containsObject:buddyName]) {
[onlineUsers addObject:buddyName];
[self.tView reloadData];
} } //好友下线
-(void)buddyWentOffline:(NSString *)buddyName{ [onlineUsers removeObject:buddyName];
[self.tView reloadData]; } //取得当前程序的委托
-(KKAppDelegate *)appDelegate{ return (KKAppDelegate *)[[UIApplication sharedApplication] delegate]; } //取得当前的XMPPStream
-(XMPPStream *)xmppStream{ return [[self appDelegate] xmppStream];
} //在线好友
-(void)newBuddyOnline:(NSString *)buddyName{ if (![onlineUsers containsObject:buddyName]) {
[onlineUsers addObject:buddyName];
[self.tView reloadData];
} } //好友下线
-(void)buddyWentOffline:(NSString *)buddyName{ [onlineUsers removeObject:buddyName];
[self.tView reloadData]; }
在viewDidLoad中加入 [java]
//设定在线用户委托
KKAppDelegate *del = [self appDelegate];
del.chatDelegate = self; //设定在线用户委托
KKAppDelegate *del = [self appDelegate];
del.chatDelegate = self;这两行代码,让好友列表的委托实现方法在本程序中 在viewWillAppear中加入 [java]
[super viewWillAppear:animated]; NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"]; if (login) { if ([[self appDelegate] connect]) {
NSLog(@"show buddy list"); } }else { //设定用户
[self Account:self]; } [super viewWillAppear:animated]; NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"]; if (login) { if ([[self appDelegate] connect]) {
NSLog(@"show buddy list"); } }else { //设定用户
[self Account:self]; }
判断本地保存的数据中是否有userId,没有的话就跳转到登录页面 这里最重要的就是connect了,这一句话就是登录了,成功的话,页面就会显示好友列表了。 [java]
#pragma mark UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //start a Chat
chatUserName = (NSString *)[onlineUsers objectAtIndex:indexPath.row]; [self performSegueWithIdentifier:@"chat" sender:self]; }
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.identifier isEqualToString:@"chat"]) {
KKChatController *chatController = segue.destinationViewController;
chatController.chatWithUser = chatUserName;
}
} #pragma mark UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //start a Chat
chatUserName = (NSString *)[onlineUsers objectAtIndex:indexPath.row]; [self performSegueWithIdentifier:@"chat" sender:self]; }
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.identifier isEqualToString:@"chat"]) {
KKChatController *chatController = segue.destinationViewController;
chatController.chatWithUser = chatUserName;
}
}
当显示出好友列表,我们选择一个好友进行聊天 将当前好友名称发送给聊天页面 下面是聊天Controller了 在KKChatController.h中加入 [java]
NSMutableArray *messages; NSMutableArray *messages;这是我们要显示的消息,每一条消息为一条字典 接下来就是每一条消息的显示了 [java]
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"msgCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
} NSMutableDictionary *dict = [messages objectAtIndex:indexPath.row]; cell.textLabel.text = [dict objectForKey:@"msg"];
cell.detailTextLabel.text = [dict objectForKey:@"sender"];
cell.accessoryType = UITableViewCellAccessoryNone; return cell; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"msgCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
} NSMutableDictionary *dict = [messages objectAtIndex:indexPath.row]; cell.textLabel.text = [dict objectForKey:@"msg"];
cell.detailTextLabel.text = [dict objectForKey:@"sender"];
cell.accessoryType = UITableViewCellAccessoryNone; return cell; }
跟上面好友Controller一样,这里我们也需要XMPPStream [java]
-(KKAppDelegate *)appDelegate{ return (KKAppDelegate *)[[UIApplication sharedApplication] delegate];
} -(XMPPStream *)xmppStream{ return [[self appDelegate] xmppStream];
} -(KKAppDelegate *)appDelegate{ return (KKAppDelegate *)[[UIApplication sharedApplication] delegate];
} -(XMPPStream *)xmppStream{ return [[self appDelegate] xmppStream];
}
在ViewDidLoad中加入 [java]
KKAppDelegate *del = [self appDelegate];
del.messageDelegate = self; KKAppDelegate *del = [self appDelegate];
del.messageDelegate = self;
设定消息委托由自己来接收和处理 #pragma mark KKMessageDelegate
···objectc
-(void)newMessageReceived:(NSDictionary *)messageCotent{ [messages addObject:messageCotent]; [self.tView reloadData]; } #pragma mark KKMessageDelegate
-(void)newMessageReceived:(NSDictionary *)messageCotent{ [messages addObject:messageCotent]; [self.tView reloadData]; } 接下来最重要的就是发送消息了 - (IBAction)sendButton:(id)sender { //本地输入框中的信息
NSString *message = self.messageTextField.text; if (message.length > 0) { //XMPPFramework主要是通过KissXML来生成XML文件
//生成<body>文档
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message]; //生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:chatWithUser];
//由谁发送
[mes addAttributeWithName:@"from" stringValue:[[NSUserDefaults standardUserDefaults] stringForKey:USERID]];
//组合
[mes addChild:body]; //发送消息
[[self xmppStream] sendElement:mes]; self.messageTextField.text = @"";
[self.messageTextField resignFirstResponder]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:message forKey:@"msg"];
[dictionary setObject:@"you" forKey:@"sender"]; [messages addObject:dictionary]; //重新刷新tableView
[self.tView reloadData]; } }
05-02 01:26