




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、iOS的xmppframework簡介1登錄和好友上下線1.1XMPP中常用對象們XMPPStream:xmpp基礎(chǔ)服務(wù)類XMPPRoster:好友列表類XMPPRosterCoreDataStorage:好友列表(用戶賬號)在core data中的操作類XMPPvCardCoreDataStorage:好友名片(昵稱,簽名,性別,年齡等信息)在core data中的操作類XMPPvCardTemp:好友名片實體類,從數(shù)據(jù)庫里取出來的都是它xmppvCardAvatarModule:好友頭像XMPPReconnect:如果失去連接,自動重連XMPPRoom:提供多用戶聊天支持XMPPPubSu
2、b:發(fā)布訂閱1.2登錄操作,也就是連接xmpp服務(wù)器- (void)connect if (self.xmppStream = nil) self.xmppStream = XMPPStream alloc init; self.xmppStream
3、addDelegate:self delegateQueue:dispatch_get_main_queue(); if (!self.xmppStream isConnected) NSString *username = NSUserDefaults standardUserDefaults objectForKe
4、y:"username" XMPPJID *jid = XMPPJID jidWithUser:username domain:"lizhen" resource:"Ework" self.xmppStream setMyJID:jid;
5、0; self.xmppStream setHostName:"10.4.125.113" NSError *error = nil; if (!self.xmppStream connect:&error)
6、; NSLog("Connect Error: %", error userInfo description); connect成功之后會依次調(diào)用XMPPStreamDelegate的方法,首先調(diào)用- (void)xmppStream:(XMPPStream *)sender socketDidConne
7、ct:(GCDAsyncSocket *)socket然后- (void)xmppStreamDidConnect:(XMPPStream *)sender在該方法下面需要使用xmppStream 的authenticateWithPassword方法進(jìn)行密碼驗證,成功的話會響應(yīng)delegate的方法,就是下面這個- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender1.3上線實現(xiàn) - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender 委托方法-
8、;(void)xmppStreamDidAuthenticate:(XMPPStream *)sender XMPPPresence *presence = XMPPPresence presenceWithType:"available" self.xmppStream sendElement:presence;1.4退出并斷開連接- (void)disconnect
9、; XMPPPresence *presence = XMPPPresence presenceWithType:"unavailable" self.xmppStream sendElement:presence; self.xmppStream disconnect;1.5好友狀態(tài)獲取好友狀態(tài),通過實現(xiàn) - (void)xmppSt
10、ream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence方法當(dāng)接收到 <presence /> 標(biāo)簽的內(nèi)容時,XMPPFramework 框架回調(diào)該方法 一個 <presence /> 標(biāo)簽的格式一般如下:<presence from=""><show>這里是顯示的內(nèi)容<show /><status>這里是顯示的狀態(tài)<status /><
11、;presence />presence 的狀態(tài):available 上線away 離開do not disturb 忙碌unavailable 下線- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence NSString *presenceType = presence type; N
12、SString *presenceFromUser = presence from user; if (!presenceFromUser isEqualToString:sender myJID user) if (presenceType isEqualToString:"available")
13、60; / else if (presenceType isEqualToString:"unavailable") /
14、160; 2接收消息和發(fā)送消息2.1接收消息通過實現(xiàn) - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message;方法當(dāng)接收到 <message /> 標(biāo)簽的內(nèi)容時,XMPPFramework 框架回調(diào)該方法根據(jù) XMPP 協(xié)議,消息體的內(nèi)容存儲在標(biāo)簽 <body /> 內(nèi)- (void)xmppStream:(XMP
15、PStream *)sender didReceiveMessage:(XMPPMessage *)message NSString *messageBody = message elementForName:"body" stringValue;2.2發(fā)送消息發(fā)送消息,我們需要根據(jù) XMPP 協(xié)議,將數(shù)據(jù)放到 <message /> 標(biāo)簽內(nèi),例如:<message type="chat"&
16、#160;to="xiaoming"><body>Hello World!<body /><message />- (void)sendMessage:(NSString *) message toUser:(NSString *) user NSXMLElement *body = NSXMLElement elementWithName:&qu
17、ot;body" body setStringValue:message; NSXMLElement *message = NSXMLElement elementWithName:"message" message addAttributeWithName:"type" stringValue:"chat"
18、 NSString *to = NSString stringWithFormat:"%", user; message addAttributeWithName:"to" stringValue:to; message addChild:body; self.xmppStream sendEle
19、ment:message;3獲取好友信息和刪除好友3.1好友列表和好友名片_xmppRoster fetchRoster;/獲取好友列表/獲取到一個好友節(jié)點- (void)xmppRoster:(XMPPRoster *)sender didRecieveRosterItem:(NSXMLElement *)item/獲取完好友列表- (void)xmppRosterDidEndPopulating:(XMPPRoster *)sender/到服務(wù)器上請求聯(lián)系人名片信息- (void)fetchvCardTempF
20、orJID:(XMPPJID *)jid;/請求聯(lián)系人的名片,如果數(shù)據(jù)庫有就不請求,沒有就發(fā)送名片請求- (void)fetchvCardTempForJID:(XMPPJID *)jid ignoreStorage:(BOOL)ignoreStorage;/獲取聯(lián)系人的名片,如果數(shù)據(jù)庫有就返回,沒有返回空,并到服務(wù)器上抓取- (XMPPvCardTemp *)vCardTempForJID:(XMPPJID *)jid shouldFetch:(BOOL)shouldFetch;/更新自己的名片信息-
21、(void)updateMyvCardTemp:(XMPPvCardTemp *)vCardTemp;/獲取到一盒聯(lián)系人的名片信息的回調(diào)- (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp
22、0; forJID:(XMPPJID *)jid3.2添加好友 /name為用戶賬號 - (void)XMPPAddFriendSubscribe:(NSString *)name
23、 /XMPPHOST 就是服務(wù)器名, 主機(jī)名 XMPPJID *jid = XMPPJID jidWithString:NSString stringWithFormat:"%",name,XMPPHOST; /presence addAttributeWithName:"sub
24、scription" stringValue:"好友" xmppRoster subscribePresenceToUser:jid; 3.3收到添加好友的請求 - (void)xmppRoster:(XMPPRoster *)sender
25、0;didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence /取得好友狀態(tài) NSString *presenceType = NSString stringWithFormat:"%", presence
26、;type; /online/offline /請求的用戶 NSString *presenceFromUser =NSString stringWithFormat:"%", presence from user; NSLog
27、("presenceType:%",presenceType); NSLog("presence2:% sender2:%",presence,sender);
28、0;XMPPJID *jid = XMPPJID jidWithString:presenceFromUser; /接收添加好友請求 xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES; &
29、#160; 3.4刪除好友/刪除好友,name為好友賬號- (void)removeBuddy:(NSString *)name XMPPJID *jid = XMPPJID jidWithString:NSString stringWithFormat:"%",name,XMPPHOST;
30、60; self xmppRoster removeUser:jid; 4聊天室 /初始化聊天室 XMPPJID *roomJID = XMPPJID jidWithString:ROOM_JID; xmppRoom = XMPPRoom alloc&
31、#160;initWithRoomStorage:self jid:roomJID; xmppRoom activate:xmppStream; xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue(); /創(chuàng)建聊天室成功 - (void)xmppRoomDidCreate:(X
32、MPPRoom *)sender DDLogInfo("%: %", THIS_FILE, THIS_METHOD); /加入聊天室,使用昵稱 xmppRoom joinRoomUsingNickname:&q
33、uot;quack" history:nil; /獲取聊天室信息 - (void)xmppRoomDidJoin:(XMPPRoom *)sender xmppRoom fetchConfigurationForm; xmppRoom
34、0;fetchBanList; xmppRoom fetchMembersList; xmppRoom fetchModeratorsList; 如果房間存在,會調(diào)用委托 / 收到禁止名單列表 - (void)xmppRoom:(XM
35、PPRoom *)sender didFetchBanList:(NSArray *)items; / 收到好友名單列表 - (void)xmppRoom:(XMPPRoom *)sender didFetchMembersList:(NSArray *)items; / 收到主持人名單列表 - (void)xmp
36、pRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items;房間不存在,調(diào)用委托 - (void)xmppRoom:(XMPPRoom *)sender didNotFetchBanList:(XMPPIQ *)iqError; - (void)xmppRoom:(XMPPRoom *)sender didNotFetchMembers
37、List:(XMPPIQ *)iqError; - (void)xmppRoom:(XMPPRoom *)sender didNotFetchModeratorsList:(XMPPIQ *)iqError;離開房間xmppRoom deactivate:xmppStream;XMPPRoomDelegate的其他代理方法 /離開聊天室 - (void)xmppRoomDidLeave:(XMPPRoom
38、160;*)sender DDLogVerbose("%: %", THIS_FILE, THIS_METHOD); /新人加入群聊 - (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID
39、160;*)occupantJID DDLogVerbose("%: %", THIS_FILE, THIS_METHOD); /有人退出群聊 - (void)xmppRoom:(XMPPRoom *)sender occupantDidLeave
40、:(XMPPJID *)occupantJID DDLogVerbose("%: %", THIS_FILE, THIS_METHOD); /有人在群里發(fā)言 - (void)xmppRoom:(XMPPRoom *)sender didReceiv
41、eMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID DDLogVerbose("%: %", THIS_FILE, THIS_METHOD); 5消息回執(zhí)這個是XEP0184協(xié)議的內(nèi)容協(xié)議內(nèi)容:發(fā)送消息時附加回執(zhí)請求
42、0; <message from='northumberlandshakespeare.lit/westminster' id='richard2-4.1.247' to='kingrichardroyalty.england.lit/throne'> <body>My lord, dispatch;
43、read o'er these articles.</body> <request xmlns='urn:xmpp:receipts'/> </message>代碼實現(xiàn) NSString *siID = XMPPStream generateUUID; /發(fā)送消息
44、 XMPPMessage *message = XMPPMessage messageWithType:"chat" to:jid elementID:siID; NSXMLElement *receipt = NSXMLElement elementWithName:"request" xmlns:"urn:xmpp:receipts"
45、; message addChild:receipt; message addBody:"測試" self.xmppStream sendElement:message;收到回執(zhí)請求的消息,發(fā)送回執(zhí)<message from='kingrichardroyalty.england.lit/throne' id
46、='bi29sg183b4v' to='northumberlandshakespeare.lit/westminster'> <received xmlns='urn:xmpp:receipts' id='richard2-4.1.247'/> </message>代碼實現(xiàn) - (void
47、)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message /回執(zhí)判斷 NSXMLElement *request = message elementForName:"request"&
48、#160; if (request) if (request.xmlns isEqualToString:"urn:xmpp:receipts")/消息回執(zhí) &
49、#160; /組裝消息回執(zhí) XMPPMessage *msg = XMPPMessage messageWithType
50、:message attributeStringValueForName:"type" to:message.from elementID:message attributeStringValueForName:"id" NSXMLElement *recieved = NSXMLElement
51、160;elementWithName:"received" xmlns:"urn:xmpp:receipts" msg addChild:recieved;
52、 /發(fā)送回執(zhí) self.xmppStream sendElement:msg;
53、 else NSXMLElement *received = message elementForName:"received"
54、0; if (received) if (received.xmlns isEqualToString:"urn:xmpp:receipts")/
55、消息回執(zhí) /發(fā)送成功
56、60; NSLog("message send success!");
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 住宅室內(nèi)裝修合同
- 三農(nóng)村基層法治建設(shè)與實踐方案
- 模板安裝施工方案
- 建筑施工工程承包合同條例
- 鋪筑施工方案
- 洗手間防水卷材的施工方案
- 《大數(shù)據(jù)技術(shù)導(dǎo)論》-教案
- 安徽省宿州市靈璧縣2024-2025學(xué)年上學(xué)期八年級數(shù)學(xué)期末試卷(原卷版+解析版)
- 自貢賓館消防施工方案
- 年產(chǎn)1000噸微生物菌劑項目環(huán)評報告表
- 橫河氧量變送器標(biāo)定及檢修
- 沉降觀測常用表格
- ArcGIS應(yīng)用基礎(chǔ)培訓(xùn)(共98張)
- 建設(shè)工程規(guī)劃放線、驗線申請表
- 南京鼓樓區(qū)部編版五年級語文下冊第二單元教材分析
- 績效考核 五金廠績效考核
- 金合極思打板與放碼系統(tǒng)幫助目錄
- 勵磁系統(tǒng)檢修規(guī)程
- 武術(shù)健身操教案《旭日東升》(共18頁)
- WE-100B300B600B1000B型萬能材料試驗機(jī)使用說明書
- 相聲《治病》
評論
0/150
提交評論