XMPPFramework是一個OS X/iOS平台的開源項目,使用Objective-C實現了XMPP協議(RFC-3920),同時還提供了用於讀寫XML的工具,大大簡化了基於XMPP的通信應用的開發。
1. 登錄和好友上下線
1.1XMPP中常用對象們
XMPPStream:xmpp基礎服務類
XMPPRoster:好友列表類
XMPPRosterCoreDataStorage:好友列表(用戶賬號)在core data中的操作類
XMPPvCardCoreDataStorage:好友名片(昵稱,簽名,性別,年齡等信息)在core data中的操作類
XMPPvCardTemp:好友名片實體類,從數據庫裡取出來的都是它
xmppvCardAvatarModule:好友頭像
XMPPReconnect:如果失去連接,自動重連
XMPPRoom:提供多用戶聊天支持
1.2登錄操作,也就是連接xmpp服務器
-(void)connect{ if(self.xmppStream==nil){ self.xmppStream=[[XMPPStreamalloc]init]; [self.xmppStreamaddDelegate:selfdelegateQueue:dispatch_get_main_queue()]; } if(![self.xmppStreamisConnected]){ NSString*username=[[NSUserDefaultsstandardUserDefaults]objectForKey:@"username"]; XMPPJID*jid=[XMPPJIDjidWithUser:usernamedomain:@"lizhen"resource:@"Ework"]; [self.xmppStreamsetMyJID:jid]; [self.xmppStreamsetHostName:@"10.4.125.113"]; NSError*error=nil; if(![self.xmppStreamconnect:&error]){ NSLog(@"ConnectError:%@",[[erroruserInfo]description]); } } }
connect成功之後會依次調用XMPPStreamDelegate的方法,首先調用
-(void)xmppStream:(XMPPStream*)sendersocketDidConnect:(GCDAsyncSocket*)socket ...
然後
-(void)xmppStreamDidConnect:(XMPPStream*)sender
在該方法下面需要使用xmppStream 的authenticateWithPassword方法進行密碼驗證,成功的話會響應delegate的方法,就是下面這個
-(void)xmppStreamDidAuthenticate:(XMPPStream*)sender
1.3上線
實現- (void)xmppStreamDidAuthenticate:(XMPPStream*)sender委托方法
-(void)xmppStreamDidAuthenticate:(XMPPStream*)sender{ XMPPPresence*presence=[XMPPPresencepresenceWithType:@"available"]; [self.xmppStreamsendElement:presence]; }
1.4退出並斷開連接
-(void)disconnect{ XMPPPresence*presence=[XMPPPresencepresenceWithType:@"unavailable"]; [self.xmppStreamsendElement:presence]; [self.xmppStreamdisconnect]; }
1.5好友狀態
獲取好友狀態,通過實現
-(void)xmppStream:(XMPPStream*)senderdidReceivePresence:(XMPPPresence*)presence ...
方法,當接收到 presence 標簽的內容時,XMPPFramework 框架回調該方法
一個 presence 標簽的格式一般如下:
presence 的狀態:
available 上線
away 離開
do not disturb 忙碌
-(void)xmppStream:(XMPPStream*)senderdidReceivePresence:(XMPPPresence*)presence{ NSString*presenceType=[presencetype]; NSString*presenceFromUser=[[presencefrom]user]; if(![presenceFromUserisEqualToString:[[sendermyJID]user]]){ if([presenceTypeisEqualToString:@"available"]){ // }elseif([presenceTypeisEqualToString:@"unavailable"]){ // } } }
2. 接收消息和發送消息
2.1接收消息
通過實現
-(void)xmppStream:(XMPPStream*)senderdidReceiveMessage:(XMPPMessage*)message;
方法
當接收到 message 標簽的內容時,XMPPFramework 框架回調該方法
根據 XMPP 協議,消息體的內容存儲在標簽 body 內
-(void)xmppStream:(XMPPStream*)senderdidReceiveMessage:(XMPPMessage*)message{ NSString*messageBody=[[messageelementForName:@"body"]stringValue]; }
2.2發送消息
發送消息,我們需要根據 XMPP 協議,將數據放到標簽內,例如:
-(void)sendMessage:(NSString*)messagetoUser:(NSString*)user{ NSXMLElement*body=[NSXMLElementelementWithName:@"body"]; [bodysetStringValue:message]; NSXMLElement*message=[NSXMLElementelementWithName:@"message"]; [messageaddAttributeWithName:@"type"stringValue:@"chat"]; NSString*to=[NSStringstringWithFormat:@"%@@example.com",user]; [messageaddAttributeWithName:@"to"stringValue:to]; [messageaddChild:body]; [self.xmppStreamsendElement:message]; }
3. 獲取好友信息和刪除好友
3.1好友列表和好友名片
[_xmppRosterfetchRoster];//獲取好友列表 //獲取到一個好友節點 -(void)xmppRoster:(XMPPRoster*)senderdidRecieveRosterItem:(NSXMLElement*)item //獲取完好友列表 -(void)xmppRosterDidEndPopulating:(XMPPRoster*)sender //到服務器上請求聯系人名片信息 -(void)fetchvCardTempForJID:(XMPPJID*)jid; //請求聯系人的名片,如果數據庫有就不請求,沒有就發送名片請求 -(void)fetchvCardTempForJID:(XMPPJID*)jidignoreStorage:(BOOL)ignoreStorage; //獲取聯系人的名片,如果數據庫有就返回,沒有返回空,並到服務器上抓取 -(XMPPvCardTemp*)vCardTempForJID:(XMPPJID*)jidshouldFetch:(BOOL)shouldFetch; //更新自己的名片信息 -(void)updateMyvCardTemp:(XMPPvCardTemp*)vCardTemp; //獲取到一盒聯系人的名片信息的回調 -(void)xmppvCardTempModule:(XMPPvCardTempModule*)vCardTempModule didReceivevCardTemp:(XMPPvCardTemp*)vCardTemp forJID:(XMPPJID*)jid
3.2添加好友
//name為用戶賬號 -(void)XMPPAddFriendSubscribe:(NSString*)name { //XMPPHOST就是服務器名,主機名 XMPPJID*jid=[XMPPJIDjidWithString:[NSStringstringWithFormat:@"%@@%@",name,XMPPHOST]]; //[presenceaddAttributeWithName:@"subscription"stringValue:@"好友"]; [xmppRostersubscribePresenceToUser:jid]; }
3.3收到添加好友的請求
-(void)xmppRoster:(XMPPRoster*)senderdidReceivePresenceSubscriptionRequest:(XMPPPresence*)presence { //取得好友狀態 NSString*presenceType=[NSStringstringWithFormat:@"%@",[presencetype]];//online/offline //請求的用戶 NSString*presenceFromUser=[NSStringstringWithFormat:@"%@",[[presencefrom]user]]; NSLog(@"presenceType:%@",presenceType); NSLog(@"presence2:%@sender2:%@",presence,sender); XMPPJID*jid=[XMPPJIDjidWithString:presenceFromUser]; //接收添加好友請求 [xmppRosteracceptPresenceSubscriptionRequestFrom:jidandAddToRoster:YES]; }
3.4刪除好友
//刪除好友,name為好友賬號 -(void)removeBuddy:(NSString*)name { XMPPJID*jid=[XMPPJIDjidWithString:[NSStringstringWithFormat:@"%@@%@",name,XMPPHOST]]; [selfxmppRoster]removeUser:jid]; }
4. 聊天室
初始化聊天室
XMPPJID*roomJID=[XMPPJIDjidWithString:ROOM_JID]; xmppRoom=[[XMPPRoomalloc]initWithRoomStorage:selfjid:roomJID]; [xmppRoomactivate:xmppStream]; [xmppRoomaddDelegate:selfdelegateQueue:dispatch_get_main_queue()];
創建聊天室成功
-(void)xmppRoomDidCreate:(XMPPRoom*)sender { DDLogInfo(@"%@:%@",THIS_FILE,THIS_METHOD); }
加入聊天室,使用昵稱
[xmppRoomjoinRoomUsingNickname:@"quack"history:nil];
獲取聊天室信息
-(void)xmppRoomDidJoin:(XMPPRoom*)sender { [xmppRoomfetchConfigurationForm]; [xmppRoomfetchBanList]; [xmppRoomfetchMembersList]; [xmppRoomfetchModeratorsList]; }
如果房間存在,會調用委托
//收到禁止名單列表 -(void)xmppRoom:(XMPPRoom*)senderdidFetchBanList:(NSArray*)items; //收到好友名單列表 -(void)xmppRoom:(XMPPRoom*)senderdidFetchMembersList:(NSArray*)items; //收到主持人名單列表 -(void)xmppRoom:(XMPPRoom*)senderdidFetchModeratorsList:(NSArray*)items;
房間不存在,調用委托
-(void)xmppRoom:(XMPPRoom*)senderdidNotFetchBanList:(XMPPIQ*)iqError; -(void)xmppRoom:(XMPPRoom*)senderdidNotFetchMembersList:(XMPPIQ*)iqError; -(void)xmppRoom:(XMPPRoom*)senderdidNotFetchModeratorsList:(XMPPIQ*)iqError;
離開房間
[xmppRoomdeactivate:xmppStream];
XMPPRoomDelegate的其他代理方法:
離開聊天室
-(void)xmppRoomDidLeave:(XMPPRoom*)sender { DDLogVerbose(@"%@:%@",THIS_FILE,THIS_METHOD); }
新人加入群聊
-(void)xmppRoom:(XMPPRoom*)senderoccupantDidJoin:(XMPPJID*)occupantJID { DDLogVerbose(@"%@:%@",THIS_FILE,THIS_METHOD); }
有人退出群聊
-(void)xmppRoom:(XMPPRoom*)senderoccupantDidLeave:(XMPPJID*)occupantJID { DDLogVerbose(@"%@:%@",THIS_FILE,THIS_METHOD); }
有人在群裡發言
-(void)xmppRoom:(XMPPRoom*)senderdidReceiveMessage:(XMPPMessage*)messagefromOccupant:(XMPPJID*)occupantJID { DDLogVerbose(@"%@:%@",THIS_FILE,THIS_METHOD); }
5. 消息回執
這個是XEP-0184協議的內容
協議內容:
發送消息時附加回執請求
代碼實現
NSString*siID=[XMPPStreamgenerateUUID]; //發送消息 XMPPMessage*message=[XMPPMessagemessageWithType:@"chat"to:jidelementID:siID]; NSXMLElement*receipt=[NSXMLElementelementWithName:@"request"xmlns:@"urn:xmpp:receipts"]; [messageaddChild:receipt]; [messageaddBody:@"測試"]; [self.xmppStreamsendElement:message];
收到回執請求的消息,發送回執
代碼實現
-(void)xmppStream:(XMPPStream*)senderdidReceiveMessage:(XMPPMessage*)message { //回執判斷 NSXMLElement*request=[messageelementForName:@"request"]; if(request) { if([request.xmlnsisEqualToString:@"urn:xmpp:receipts"])//消息回執 { //組裝消息回執 XMPPMessage*msg=[XMPPMessagemessageWithType:[messageattributeStringValueForName:@"type"]to:message.fromelementID:[messageattributeStringValueForName:@"id"]]; NSXMLElement*recieved=[NSXMLElementelementWithName:@"received"xmlns:@"urn:xmpp:receipts"]; [msgaddChild:recieved]; //發送回執 [self.xmppStreamsendElement:msg]; } }else { NSXMLElement*received=[messageelementForName:@"received"]; if(received) { if([received.xmlnsisEqualToString:@"urn:xmpp:receipts"])//消息回執 { //發送成功 NSLog(@"messagesendsuccess!"); } } } //消息處理 //... }
6. 添加AutoPing
為了監聽服務器是否有效,增加心跳監聽。用XEP-0199協議,在XMPPFrameWork框架下,封裝了XMPPAutoPing 和XMPPPing兩個類都可以使用,因為XMPPAutoPing已經組合進了XMPPPing類,所以XMPPAutoPing使用起來更方便。
//初始化並啟動ping -(void)autoPingProxyServer:(NSString*)strProxyServer { _xmppAutoPing=[[XMPPAutoPingalloc]init]; [_xmppAutoPingactivate:_xmppStream]; [_xmppAutoPingaddDelegate:selfdelegateQueue:dispatch_get_main_queue()]; _xmppAutoPing.respondsToQueries=YES; _xmppAutoPing.pingInterval=2;//ping間隔時間 if(nil!=strProxyServer) { _xmppAutoPing.targetJID=[XMPPJIDjidWithString:strProxyServer];//設置ping目標服務器,如果為nil,則監聽socketstream當前連接上的那個服務器 } } //卸載監聽 [_xmppAutoPingdeactivate]; [_xmppAutoPingremoveDelegate:self]; _xmppAutoPing=nil; //pingXMPPAutoPingDelegate的委托方法: -(void)xmppAutoPingDidSendPing:(XMPPAutoPing*)sender { NSLog(@"-(void)xmppAutoPingDidSendPing:(XMPPAutoPing*)sender"); } -(void)xmppAutoPingDidReceivePong:(XMPPAutoPing*)sender { NSLog(@"-(void)xmppAutoPingDidReceivePong:(XMPPAutoPing*)sender"); } -(void)xmppAutoPingDidTimeout:(XMPPAutoPing*)sender { NSLog(@"-(void)xmppAutoPingDidTimeout:(XMPPAutoPing*)sender"); }