因爲博客遷移至www.coderyi.com,文章請看http://www.coderyi.com/archives/436數據庫
XMPPStream:xmpp基礎服務類服務器
XMPPRoster:好友列表類框架
XMPPRosterCoreDataStorage:好友列表(用戶帳號)在core data中的操做類dom
XMPPvCardCoreDataStorage:好友名片(暱稱,簽名,性別,年齡等信息)在core data中的操做類socket
XMPPvCardTemp:好友名片實體類,從數據庫裏取出來的都是它測試
xmppvCardAvatarModule:好友頭像fetch
XMPPReconnect:若是失去鏈接,自動重連spa
XMPPRoom:提供多用戶聊天支持代理
XMPPPubSub:發佈訂閱code
- (void)connect { if (self.xmppStream == nil) { self.xmppStream = [[XMPPStream alloc] init]; [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()]; } if (![self.xmppStream isConnected]) { NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"]; XMPPJID *jid = [XMPPJID jidWithUser:username domain:@"lizhen" resource:@"Ework"]; [self.xmppStream setMyJID:jid]; [self.xmppStream setHostName:@"10.4.125.113"]; NSError *error = nil; if (![self.xmppStream connect:&error]) { NSLog(@"Connect Error: %@", [[error userInfo] description]); } } }
connect成功以後
會依次調用XMPPStreamDelegate的方法,
首先調用
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
而後
- (void)xmppStreamDidConnect:(XMPPStream *)sender
在該方法下面須要使用xmppStream 的authenticateWithPassword方法進行密碼驗證,成功的話會響應delegate的方法,就是下面這個
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
實現 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender 委託方法
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"]; [self.xmppStream sendElement:presence]; }
- (void)disconnect { XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"]; [self.xmppStream sendElement:presence]; [self.xmppStream disconnect]; }
獲取好友狀態,經過實現
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
方法
當接收到 <presence /> 標籤的內容時,XMPPFramework 框架回調該方法
一個 <presence /> 標籤的格式通常以下:
<presence from=""> <show>這裏是顯示的內容<show /> <status>這裏是顯示的狀態<status /> <presence />
presence 的狀態:
available 上線
away 離開
do not disturb 忙碌
unavailable 下線
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence { NSString *presenceType = [presence type]; NSString *presenceFromUser = [[presence from] user]; if (![presenceFromUser isEqualToString:[[sender myJID] user]]) { if ([presenceType isEqualToString:@"available"]) { // } else if ([presenceType isEqualToString:@"unavailable"]) { // } } }
經過實現
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message;
方法
當接收到 <message /> 標籤的內容時,XMPPFramework 框架回調該方法
根據 XMPP 協議,消息體的內容存儲在標籤 <body /> 內
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { NSString *messageBody = [[message elementForName:@"body"] stringValue]; }
發送消息,咱們須要根據 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]; }
[_xmppRoster fetchRoster];//獲取好友列表 //獲取到一個好友節點 - (void)xmppRoster:(XMPPRoster *)sender didRecieveRosterItem:(NSXMLElement *)item //獲取無缺友列表 - (void)xmppRosterDidEndPopulating:(XMPPRoster *)sender //到服務器上請求聯繫人名片信息 - (void)fetchvCardTempForJID:(XMPPJID *)jid; //請求聯繫人的名片,若是數據庫有就不請求,沒有就發送名片請求 - (void)fetchvCardTempForJID:(XMPPJID *)jid ignoreStorage:(BOOL)ignoreStorage; //獲取聯繫人的名片,若是數據庫有就返回,沒有返回空,併到服務器上抓取 - (XMPPvCardTemp *)vCardTempForJID:(XMPPJID *)jid shouldFetch:(BOOL)shouldFetch; //更新本身的名片信息 - (void)updateMyvCardTemp:(XMPPvCardTemp *)vCardTemp; //獲取到一盒聯繫人的名片信息的回調 - (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp forJID:(XMPPJID *)jid
//name爲用戶帳號 - (void)XMPPAddFriendSubscribe:(NSString *)name { //XMPPHOST 就是服務器名, 主機名 XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",name,XMPPHOST]]; //[presence addAttributeWithName:@"subscription" stringValue:@"好友"]; [xmppRoster subscribePresenceToUser:jid]; }
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence { //取得好友狀態 NSString *presenceType = [NSString stringWithFormat:@"%@", [presence type]]; //online/offline //請求的用戶 NSString *presenceFromUser =[NSString stringWithFormat:@"%@", [[presence from] user]]; NSLog(@"presenceType:%@",presenceType); NSLog(@"presence2:%@ sender2:%@",presence,sender); XMPPJID *jid = [XMPPJID jidWithString:presenceFromUser]; //接收添加好友請求 [xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES]; }
3.4刪除好友
//刪除好友,name爲好友帳號
- (void)removeBuddy:(NSString *)name { XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",name,XMPPHOST]]; [self xmppRoster] removeUser:jid]; }
//初始化聊天室
XMPPJID *roomJID = [XMPPJID jidWithString:ROOM_JID]; xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self jid:roomJID]; [xmppRoom activate:xmppStream]; [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
//建立聊天室成功
- (void)xmppRoomDidCreate:(XMPPRoom *)sender { DDLogInfo(@"%@: %@", THIS_FILE, THIS_METHOD); }
//加入聊天室,使用暱稱 [xmppRoom joinRoomUsingNickname:@"quack" history:nil];
//獲取聊天室信息
- (void)xmppRoomDidJoin:(XMPPRoom *)sender { [xmppRoom fetchConfigurationForm]; [xmppRoom fetchBanList]; [xmppRoom fetchMembersList]; [xmppRoom fetchModeratorsList]; }
若是房間存在,會調用委託
// 收到禁止名單列表 - (void)xmppRoom:(XMPPRoom *)sender didFetchBanList:(NSArray *)items; // 收到好友名單列表 - (void)xmppRoom:(XMPPRoom *)sender didFetchMembersList:(NSArray *)items; // 收到主持人名單列表 - (void)xmppRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items;
房間不存在,調用委託
- (void)xmppRoom:(XMPPRoom *)sender didNotFetchBanList:(XMPPIQ *)iqError; - (void)xmppRoom:(XMPPRoom *)sender didNotFetchMembersList:(XMPPIQ *)iqError; - (void)xmppRoom:(XMPPRoom *)sender didNotFetchModeratorsList:(XMPPIQ *)iqError;
離開房間
[xmppRoom deactivate:xmppStream];
XMPPRoomDelegate的其餘代理方法
//離開聊天室
- (void)xmppRoomDidLeave:(XMPPRoom *)sender { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); }
//新人加入羣聊
- (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID *)occupantJID { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); }
//有人退出羣聊
- (void)xmppRoom:(XMPPRoom *)sender occupantDidLeave:(XMPPJID *)occupantJID { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); }
//有人在羣裏發言
- (void)xmppRoom:(XMPPRoom *)sender didReceiveMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); }
這個是XEP-0184協議的內容
協議內容:
發送消息時附加回執請求
<message from='northumberland@shakespeare.lit/westminster' id='richard2-4.1.247' to='kingrichard@royalty.england.lit/throne'> <body>My lord, dispatch; read o'er these articles.</body> <request xmlns='urn:xmpp:receipts'/> </message>
代碼實現
NSString *siID = [XMPPStream generateUUID]; //發送消息 XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:jid elementID:siID]; NSXMLElement *receipt = [NSXMLElement elementWithName:@"request" xmlns:@"urn:xmpp:receipts"]; [message addChild:receipt]; [message addBody:@"測試"]; [self.xmppStream sendElement:message];
收到回執請求的消息,發送回執
<message from='kingrichard@royalty.england.lit/throne' id='bi29sg183b4v' to='northumberland@shakespeare.lit/westminster'> <received xmlns='urn:xmpp:receipts' id='richard2-4.1.247'/> </message>
代碼實現
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { //回執判斷 NSXMLElement *request = [message elementForName:@"request"]; if (request) { if ([request.xmlns isEqualToString:@"urn:xmpp:receipts"])//消息回執 { //組裝消息回執 XMPPMessage *msg = [XMPPMessage messageWithType:[message attributeStringValueForName:@"type"] to:message.from elementID:[message attributeStringValueForName:@"id"]]; NSXMLElement *recieved = [NSXMLElement elementWithName:@"received" xmlns:@"urn:xmpp:receipts"]; [msg addChild:recieved]; //發送回執 [self.xmppStream sendElement:msg]; } }else { NSXMLElement *received = [message elementForName:@"received"]; if (received) { if ([received.xmlns isEqualToString:@"urn:xmpp:receipts"])//消息回執 { //發送成功 NSLog(@"message send success!"); } } } //消息處理 //... }
爲了監聽服務器是否有效,增長心跳監聽。用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 = [XMPPJID jidWithString: strProxyServer ];//設置ping目標服務器,若是爲nil,則監聽socketstream當前鏈接上的那個服務器 } } //卸載監聽 [_xmppAutoPing deactivate]; [_xmppAutoPing removeDelegate:self]; _xmppAutoPing = nil; //ping XMPPAutoPingDelegate的委託方法: - (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"); }