XMPP protocol for Instant Messaging underlying writing (ii)--IOS xmppframework demo+ Analysis

Source: Internet
Author: User
Tags vcard

I hope that this is a new day!

Before I look at the code, I think you should tidy up your mood and listen to my words:

First of all, I hope you read this blog in the morning, and then start the operation, if you just look at the blog and not to compare their own projects, the role is not very large. Set off, so with a technical desire, excitement, excitement to learn, you can have the income. Well, take my humble time to do the project, the first thing to get up every morning is to study the code of Xmppframework author, analyze and imitate the writing according to the module, and think, analyze and summarize when sleeping.


Of course I'm not saying that every dev is going to do this to me, just hoping you can keep 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 products, then you need to sometimes to see if the original author is fix Some bugs)


Iphonexmpp Demo

1.appdelegate.m

A. Probably look at the head file, OK, do not jump in-depth look, below I will teach a quick look. See this method


There are several places to note:

1) Ddlog used not to insist, I like the clean and refreshing console, so it is useless this thing, because I am not very dependent on all dozen log, but the breakpoint console po xxx method, real-time to find the problem fix bug

2) Configure the XML stream stream to give you a long connection inside to add a variety of buff, various equipment, various properties. OK, no kidding:), this configuration is important, it determines which XMPP services your app needs to support, and determines which code function modules the original author (C.H. Robinson) does not need to take effect

3) Start the connection, of course, there is also a cancel Connect


B Set your XML Stream to turn on which features

-(void) Setupstream{nsassert (Xmppstream = = nil, @ "Method Setupstream invoked multiple times");//Setup XMPP stream////Th E Xmppstream is the base class for all activity.//everything else plugs into the xmppstream, such as Modules/extensions a nd 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 was foregrounded again.//We are Patie ntly waiting for a fix from apple.//If you don't enablebackgroundingonsocket on the simulator,//you'll simp ly see a 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 bu Nch more information in the Xmppreconnect header file.xmppreconnect = [[Xmppreconnect alloc] init];//Setup roster////Th E Xmpproster handles the XMPP protocol stuff related to the roster.//of the storage for the roster are abstracted.//so you c An If storage mechanism you want.//you can store it all in memory, or use the core data and store it on disk, or use Cor e data with a in-memory store,//or setup your own using raw SQLite, or create your own storage mechanism.//you can do I T however you like! It's your application.//but you did need to provide the roster with some storage facility.xmpprosterstorage = [[Xmpproster Coredatastorage 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 vCa  Rd Temp module To download user avatars.//the Xmpproster would 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 PR Esence on the network//They include information about what capabilities their client supports (audio, video, file Transfe R, etc).//But as 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 is going to has the same list of capabilities.//so the Protocol defines a standardized a-Z to hash the list of capabilities.//clients then broadcast the tiny hash instead of the big list.//the Xmppcapabilities protocol automatical  LY 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 be strongly encouraged to persist C APS information across sessions.////The Xmppcapabilitiescoredatastorage is a ideal solution.//It can also be shared am Ongst multiple streams to further reduce hash lookups.xmppcapabilitiesstorage = [xmppcapabilitiescoredatastorage    Sharedinstance];    Xmppcapabilities = [[Xmppcapabilities alloc] initwithcapabilitiesstorage:xmppcapabilitiesstorage];    Xmppcapabilities.autofetchhashedcapabilities = YES;         Xmppcapabilities.autofetchnonhashedcapabilities = no;//Activate xmpp modules[xmppreconnectActivate:xmppstream]; [Xmpproster Activate:xmppstream]; [Xmppvcardtempmodule Activate:xmppstream]; [Xmppvcardavatarmodule Activate:xmppstream]; [Xmppcapabilities activate:xmppstream];//Add ourself as a delegate to anything we'll be interested In[xmppstream ad Ddelegate: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'll be automatically resolved using the JID (below).//For example, if you supply a JID like ' [email protected]/ RSRC '//Then the XMPP framework would follow the XMPP specification, and do a SRV lookup for quack.com.////If you don ' t s Pecify a hostport, then the default (5222) would be Used.//[xmppstream sethostname:@ "talk.google.com"];//[xmppstream Setho stport:5222];//need to alter these settings depending on tHe server you ' re connecting tocustomcertevaluation = YES;} 

Ok,let ' s begin.

1) Create an XML stream object, what is the goods for? Make a metaphor: freight transport belt, goods and annealed to rely on this tape. Who let this tape move?

Long connections

It's a motor.

So what's in the goods? 3 Kinds of Goods: American version, Hong Kong version, Japanese version, occasionally with a point of the country line. (*^__^*) hehe ..., haha no kidding. There are three kinds of goods:<iq> <p><message>, occasionally with some other labels <a><r> something.

Sodasne! ~ KO One! ~ Yes, good, great! ~ Found yesterday RFC6121 a bit of a relationship. ~\ (≧▽≦)/~ la La.

2) whether to turn on the background mode---NO, unless you have VoIP need to support, otherwise endless, now GitHub on the backstage issue still have a lot of unresolved, anyway it is very complicated, I this dish forced not support this goods

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

3) Turn on the reconnection mechanism (long connection necessary, heartbeat what)

Open roster (two forms: CoreData storage or open memory-temporary object storage), automatically get the roster data on the server? Whether to automatically answer a subscription request for a small partner that has subscribed to the presence message, that is, whether to automatically filter out subscriptions that have already been subscribed to, or a user request that has already formed a subscription (the difficult, later chapters are detailed). Open roster Coredatastorage, which is the data inventory coredata storage technology.


Open vcard (Personal information details) module, two times package vcard module on, and start the vcard corresponding CoreData storage technology

Open the Capabilitie, and the storage technology with it. The goods looked at the beginning of a long time, but now really forget ... Sorry, this piece needs to find the corresponding XEP to carry on the brain to fill, but seemingly temporarily cannot use, let it default.

Original link Portal

Here is a core of xmppframework: Multi Delegate (author exclusive, 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,

A sub delegate callback is made to the roster, in the current class, the main thread queue.

About multi delegate technology, it is recommended to take a good look inside the concrete implementation, do not require you will write, probably the core thought can read, will return BOOL YES or NO can ...



BTW1: I worship this multi delegate again, of course, he wrote a lot of things, are let me worship. such as the socket link ... XML parsing, Ddlog ... Anyway, a lot of good, do not say, said more are tears, vegetable dogs can only look at the great God.

BTW2: Brush brushes Two hours passed, wash and sleep, write blog really tired.

BTW3: server that water? I ask you, your environment is well equipped no, I want to example test connection, give an account and password. Labor efficiency is so high that the basic data connection is done one day.

BTW4: Manager, my side is still smooth, about small into, you see this connection interface, became, although the interface ugly point, but can connect on the service side, awesome.

BTW5: PA! You idiot, don't say this demo, how to have you such a teammate.

BTW6: Next preview <ios xmppframework--im underlying architecture design + Technical Preparation >

XMPP protocol for Instant Messaging underlying writing (ii)--IOS xmppframework demo+ Analysis

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.