XMPP協議實現即時通訊底層書寫 (二)-- IOS XMPPFramework Demo+分析

來源:互聯網
上載者:User

標籤:ant   sel   資訊   ppc   tar   解析   oid   rest   setting   

我希望,This is a new day! 

在看代碼之前,我認為你還是應該先整理一下心情,來聽我說幾句:

首先,我希望你是在早上邊看這篇blog,然後一邊開始動手操作,假設你僅僅是看blog而不去自己對照項目,作用不是非常大。一日之計在於晨,所以懷著一顆對技術渴望,激動的。亢奮的心情去學習。你才幹有所得。

嗯,就拿鄙人當時做項目來說,每天早上起來的第一件事情。就是研究XMPPFramework作者的代碼,依照模組來分析和模仿書寫。睡覺的時候還在思考,分析。總結...


當然我並非說每一個Dev 都要向我這樣,僅僅是希望你能保持一顆積極向上的心態去對待技術,對待你的工作。

that‘s all。


ResourceURL:https://github.com/robbiehanson/XMPPFramework  (假設你還在維護你現有的基於XMPP的產品。那麼你須要sometimes 去查看,原作者是否fix 一些bug)


IphoneXMPP Demo

1.AppDelegate.m

a.大概看下標頭檔。ok,別跳轉深入看了,以下我會教高速的看。See this method


有幾個地方須要注意:

  1)DDLog 用於不用不強求,鄙人喜歡乾淨清爽的控制台。所以就沒用這玩意,由於我並非非常依賴所有打log。而是斷點控制台po XXX方法,即時性找出問題修複bug

  2)配置XML Stream 流 ,給你的長串連裡面添加各種 buff。各種裝備,各種屬性。ok,不開玩笑了:),這個配置非常重要,它決定了你的app須要支援哪些xmpp服務,決定了原作者(羅賓遜)哪些代碼功能模組是不須要生效的

  3)啟動串連,當然相應的也有一個cancel connect


b 設定你的 XML Stream 。開啟哪些功能

- (void)setupStream{NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");// Setup xmpp stream// // The XMPPStream is the base class for all activity.// Everything else plugs into the xmppStream, such as modules/extensions and delegates.xmppStream = [[XMPPStream alloc] init];#if !TARGET_IPHONE_SIMULATOR{// Want xmpp to run in the background?

// // P.S. - The simulator doesn‘t support backgrounding yet.// When you try to set the associated property on the simulator, it simply fails.// And when you background an app on the simulator,// it just queues network traffic til the app is foregrounded again.// We are patiently waiting for a fix from Apple.// If you do enableBackgroundingOnSocket on the simulator,// you will simply see an error message from the xmpp stack when it fails to set the property.<span style="color:#66ff99;">xmppStream.enableBackgroundingOnSocket = YES;</span>}#endif// Setup reconnect// // The XMPPReconnect module monitors for "accidental disconnections" and// automatically reconnects the stream for you.// There‘s a bunch more information in the XMPPReconnect header file.xmppReconnect = [[XMPPReconnect alloc] init];// Setup roster// // The XMPPRoster handles the xmpp protocol stuff related to the roster.// The storage for the roster is abstracted.// So you can use any storage mechanism you want.// You can store it all in memory, or use core data and store it on disk, or use core data with an in-memory store,// or setup your own using raw SQLite, or create your own storage mechanism.// You can do it however you like! It‘s your application.// But you do need to provide the roster with some storage facility.xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];//xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];xmppRoster.autoFetchRoster = YES;xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;// Setup vCard support// // The vCard Avatar module works in conjuction with the standard vCard Temp module to download user avatars.// The XMPPRoster will automatically integrate with XMPPvCardAvatarModule to cache roster photos in the roster.xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];</span>// Setup capabilities// // The XMPPCapabilities module handles all the complex hashing of the caps protocol (XEP-0115).// Basically, when other clients broadcast their presence on the network// they include information about what capabilities their client supports (audio, video, file transfer, etc).// But as you can imagine, this list starts to get pretty big.// This is where the hashing stuff comes into play.// Most people running the same version of the same client are going to have the same list of capabilities.// So the protocol defines a standardized way to hash the list of capabilities.// Clients then broadcast the tiny hash instead of the big list.// The XMPPCapabilities protocol automatically handles figuring out what these hashes mean,// and also persistently storing the hashes so lookups aren‘t needed in the future.// // Similarly to the roster, the storage of the module is abstracted.// You are strongly encouraged to persist caps information across sessions.// // The XMPPCapabilitiesCoreDataStorage is an ideal solution.// It can also be shared amongst multiple streams to further reduce hash lookups.xmppCapabilitiesStorage = [XMPPCapabilitiesCoreDataStorage sharedInstance]; xmppCapabilities = [[XMPPCapabilities alloc] initWithCapabilitiesStorage:xmppCapabilitiesStorage]; xmppCapabilities.autoFetchHashedCapabilities = YES; xmppCapabilities.autoFetchNonHashedCapabilities = NO;// Activate xmpp modules[xmppReconnect activate:xmppStream];[xmppRoster activate:xmppStream];[xmppvCardTempModule activate:xmppStream];[xmppvCardAvatarModule activate:xmppStream];[xmppCapabilities activate:xmppStream];// Add ourself as a delegate to anything we may be interested in[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];[xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];// Optional:// // Replace me with the proper domain and port.// The example below is setup for a typical google talk account.// // If you don‘t supply a hostName, then it will be automatically resolved using the JID (below).// For example, if you supply a JID like ‘[email protected]/rsrc‘// then the xmpp framework will follow the xmpp specification, and do a SRV lookup for quack.com.// // If you don‘t specify a hostPort, then the default (5222) will be used.//[xmppStream setHostName:@"talk.google.com"];//[xmppStream setHostPort:5222];// You may need to alter these settings depending on the server you‘re connecting tocustomCertEvaluation = YES;}


ok,let‘s begin.

1)建立一個XML stream 對象。這貨是幹嘛的呢。打個比喻:貨物運輸帶。上貨和下貨 都要靠這條帶子。誰讓這條帶子動起來?

長串連

它就是個馬達。

那麼這裡面的貨是啥呢?3種貨:美版。港版。日版,偶爾帶點國行。(*^__^*) 嘻嘻……,哈哈不開玩笑了。有三種貨:<iq> <p><message>,偶爾帶點其它標籤<a><r>什麼的。

索達斯內!

~斯ko一!~是的,好厲害好棒噠!~發現昨天看得RFC6121有點關係啦。~\(≧▽≦)/~啦啦啦

2)是否開啟後台模式---NO。除非你有VOIP須要支援,不然後患無窮,如今github 上後台issue另一大堆沒解決的呢,反正呢非常複雜噠。我這菜逼就沒支援這貨

<span style="color:#66ff99;">xmppStream.enableBackgroundingOnSocket</span>

3)開啟重連機制(長串連必備,心跳啥的)

開啟roster(兩種形式:coreData儲存 or 開闢記憶體--暫時Object Storage Service),自己主動擷取server上的roster資料?是否自己主動應答 已經存在訂閱出席訊息的小夥伴的訂閱請求。也就是說是否自己主動過濾掉已經訂閱過的訂閱或者是已經形成訂閱關係的使用者請求啦(痛點。後面章節細講)。開啟roster CoreDataStorage,也就是資料庫存CoreData儲技術。


開啟vCard(個人資訊詳情) module。二次封裝vCard Module開啟,而且啟動 vcard相應的coreData 儲存技術

開啟capabilitie,和與之的儲存技術。這貨當初看了好久噠。可是如今真忘記了。

。。sorry,這塊須要找相應的XEP來進行腦補,只是貌似臨時用不到。就讓它預設吧。

原文連結傳送門

以下是 XMPPFramework的一個核心:multi delegate (作者專屬。膜拜!

~)

[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];[xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];


對XML stream 進行加入一個Sub Delegate回調,在當前Class。在mainThread Queue中,

對roster 進行一個sub delegate回調,在當前Class,主線程隊列。

關於multi delegate 技術。建議好好看看裡面的詳細實現。不要求你會寫,大概的核心思想能看懂就成,會return BOOL YES or NO 就可以。



btw1:鄙人對這個multi delegate再次膜拜。當然他寫的非常多東西。都是讓我膜拜的。。

比方socket連結。。。

XML解析。DDLog。。

。反正好多,好了不說了。說多了都是淚,菜狗僅僅能仰望大神。。

btw2:刷刷刷兩個小時過去了,洗洗睡了,寫blog真心非常累。

btw3:server那個水?問你呢。你們環境配好了沒,我這邊要example測試串連了。快給個帳號和password。勞資效率就是這麼高。一天搞定主要的資料連線了。

btw4:經理,我這邊還算順利,約有小成,你看這串連介面,成了,儘管介面醜了點,可是能連上服務端了,真棒。

btw5:啪!你個傻蛋。別說這事demo,怎麼有你這樣的隊友。

btw6:下期預告<IOS XMPPFramework --IM底層架構設計+技術準備工作>

XMPP協議實現即時通訊底層書寫 (二)-- IOS XMPPFramework Demo+分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.