XMPP protocol for instant messaging underlying writing (2) -- IOS XMPPFramework Demo + analysis, xmppxmppframework

Source: Internet
Author: User
Tags vcard

XMPP protocol for instant messaging underlying writing (2) -- IOS XMPPFramework Demo + analysis, xmppxmppframework

I hope This is a new day!

Before reading the code, I think you should first sort out your mood and let me say a few words:

First, I hope that you will read this blog in the morning and then start to operate it. If you only read the blog and do not compare the project yourself, it will not be very helpful. One day is in the morning, so with a desire for technology, excited and excited mood to learn, you can get something. Well, the first thing I did when I started my project was to study the code of the XMPPFramework author. I analyzed and imitated the code by module, and I was still thinking about it when I went to bed, analysis, summary...


Of course, I am not saying that every Dev will treat me like this. I just hope that you can maintain a positive attitude towards technology and your work.

That's all.


ResourceURL: https://github.com/robbiehanson/XMPPFramework (if you are still maintaining your existing XMPP-based product, you need to check whether the original author fix some bugs)


IphoneXMPP Demo

1. AppDelegate. m

A. Let's take a look at the header file. OK. Don't jump into it. I will teach you how to read it quickly. See this method


Note the following:

1) DDLog is used for the clean and refreshing console. It is useless because I do not rely heavily on logging all, but on the breakpoint console po XXX method, real-time problem identification and bug fixing

2) configure the XML Stream to add various buckets, equipment, and attributes to your persistent connections. OK, no joke :). This configuration is very important. It determines which xmpp services your app needs to support and which code functional modules the original author (Robinson) does not need to take effect.

3) Start the connection. Of course, there is also a cancel connect


B. Set your XML Stream and enable some functions.

- (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 'user@quack.com/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) Create an XML stream object. Why. A metaphor: the belt for cargo transportation depends on the belt for both shipping and shipping. Who made this tape work?

Persistent connection

It is a motor.

So what is the goods here? Three types of goods: US edition, Hong Kong edition, and Japanese edition. (* ^__ ^ *) Xi ......, Haha. There are three types of goods: <iq> <p> <message>, occasionally with other labels <a> <r> or something.

Sodasne !~ Siko 1 !~ Yes, it's amazing !~ I found that RFC6121 had a relationship yesterday .~ \ (Too many rows )/~ La la

2) Whether to enable the background mode-NO. Unless you have VOIP to support, there will be endless troubles. Now there are a lot of unsolved issues on the background issue on github, which is complicated anyway, I cannot support this dish.

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

3) Enable the reconnection mechanism (essential for persistent connections, heartbeat)

Enable roster (two forms: coreData storage or open-up memory-temporary object storage) to automatically obtain roster data on the server? Whether to automatically respond to the subscribe request from a friend who has subscribed to the message, that is, whether to automatically filter out subscribed subscriptions or user requests that have formed a subscription relationship (difficult, ). Enable roster CoreDataStorage, that is, the data inventory CoreData storage technology.


Enable the vCard module, enable the secondary encapsulation of the vCard Module, and enable the coreData storage technology corresponding to the vcard.

Enable capabilitie and storage technology. I have been reading this product for a long time, but I have forgotten it now... Sorry, this part needs to find the corresponding XEP for brain supplement, but it seems that it is not available for the time being, let it default.

Original link Portal

The following is a core of XMPPFramework: multi delegate (exclusive to the author, worship !~)

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


Add a Sub Delegate callback to the XML stream. In the current Class, in the mainThread Queue,

Perform a sub delegate callback on the roster. In the current Class, the main thread queue.

For the multi delegate technology, it is recommended that you take a good look at the specific implementation, you do not need to write it, the general core idea can be understood, will return bool yes or NO...



Btw1: The monks worship this multi delegate again. Of course, many of the things he wrote Let me worship .. For example, socket link... XML parsing, DDLog... There are a lot of things. If you don't want to talk about it, it's tears. You can only look up at the Food dog ..

Btw2.

Btw3: the server's water? Ask you, if your environment is ready, I want to test the connection using example. Please give me your account and password. The labor efficiency is so high that the basic data connection is completed in one day.

Btw4.

Btw5: Pop! You are dumb. Don't say this demo. How can you have a teammate like you.

Btw6: Coming soon <IOS XMPPFramework -- IM underlying architecture design + technical preparation>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.