IOS XMPP Framework Chinese Overview

Source: Internet
Author: User

Tags: hospital sage Change oom database introduction tin App Ack

This article translates the overview of the XMPP framework section in the XMPP framework

Introduced

The framework is divided into 2 parts:
1. XMPP Core section
2. xmpp extension (ROSTER,XEP ' s, optional support tools, etc.)

XMPP core implementation of XMPP specification (RFC 3920)

Please do not confuse chat with XMPP, XMPP full name is "extensible message and existence protocol", it is a kind of universal protocol that can be used for many purposes, in fact, there are many companies using this framework, such as home Self-initiative, in the hospital to send alerts to nurses.

Extensible includes such as support for nickname brochures. Self-actively re-connects with a variety of XMPP extension implementations (XEP ' s):

XMPP Core

The XMPP core file is in a directory named "core" locally. These files contain:

    • Xmppstream
    • Xmppparser
    • Xmppjid
    • Xmppelement
    • Xmppiq
    • Xmppmessage
    • Xmpppresence
    • Xmppmodule
    • Xmpplogging
    • Xmppinternal

The core of this framework is the XMPPStream class, which is the basic class you will use for interaction. It is all about extending and defining the code to insert the class, which has some interesting features that make the framework flexible, extensible, and easy to develop on top.

These will be discussed in more detail later in the documentation.

Xmppparser is the inner class used by Xmppstream. You may have guessed what it does, and you do not need to interact with the parser in any way whatsoever.

Xmppjid provides an immutable Jid (Jabber Identifier) implementation that supports parsing Jid ' s and extracts the various parts of Jid in various forms. It complies with the Nscopying protocol. Side Jid ' s can be regarded as nsdictionary key. It also complies with the Nscoding protocol.

Xmppelement is the base class for the 3 main XMPP elements: Xmppiq, Xmppmessage & Xmpppresence. Xmppelement extends the Nsxmlelement class, so you have all the Nsxml functionality. Checks for whatever XML element. In this chapter there are many other specific descriptive narratives Elements:iq, Message, & presence.

Xmppmodule provides the base class for optional implant extensions, assuming that you are writing code that your own app executes.

You are very likely to only create your own classes. Register accept proxy calls. But suppose you are implementing a standard XEP.

Or you want your application to specify that the extension is available for implantation, then you want to create it on the Xmppmodule, the module is introduced more specifically in the back.

Xmpplogging provides a very fast, powerful and flexible log framework that will be discussed in the XMPP Logging chapter.

Xmppinternal but something about the inner core and various advanced underlying extensions.

Elements: IQ, Message, & Presence

Xmppelement extends the Nsxmlelement class. So you have all the Nsxml functions, check whatever XML element.

    • Xmppiq, Xmppelement, Nsxmlelement, Nsxmlnode, NSObject
    • Xmppmessage, Xmppelement, Nsxmlelement, Nsxmlnode, NSObject
    • Xmpppresence, Xmppelement, Nsxmlelement, Nsxmlnode, NSObject

In addition to the Nsxml basic functionality, the NSXMLELEMENT+XMPP category is available.

This category provides a variety of convenient ways to make your code more concise and readable.

Like what:

[element attributeIntValueForName:@"age"];

A lot of other information please see Working with elements page

Xmppstream Configuration

An XMPP stream configuration initialization is divided into several sections:

    • Configure how to connect to Xmppserver
    • Join the agent
    • Join modules
    • Connection
    • Certification
Configure the connection

For most people. This involves only one step-set the Myjid property of the stream.

Like what:

xmppStream.myJID = [XMPPJID jidWithString:@"[email protected]"];

The XMPP stream will look for the remaining information that follows the XMPP RFC, which contains the SRV lookup _xmpp-client._tcp.domain in progress. In the example above, using Gmail, Google server may return a similar "talk.google.com". The XMPP stream will then be linked to the server, assuming that the SRV lookup lookup fails, then the XMPP stream will simply be linked to Jid's domain.

Suppose you know that you are connecting to a xmppserver that does not have an XMPP SRV record. You can tell XMPP stream to skip SRV lookups by the specified host name, such as:

xmppStream.myJID = [XMPPJID jidWithString:@"[email protected]"];xmppStream.hostName = @"myCompany.com";

The hostname will also come in handy when you use a development xmppserver. Perhaps the server is available on the behind network, or does not have a DNS address, such as:

xmppStream.myJID = [XMPPJID jidWithString:@"[email protected]"];xmppStream.hostName = @"192.168.2.27";

There is also an optional property that is host port, by default, and according to the XMPP specification. Almost all of the ports on the server are executed in 5222. Let's say your server executes on a different port. That can set the properties of the host port.

Join the agent

Xmppstream has some interesting features designed to make the framework flexible, extensible, and easy to develop on top. One of them uses MulticastDelegate.

What is MulticastDelegate?

The XMPP framework needs to support an unlimited number of extensions, which includes an official extension of the framework, as well as any number of extensions or your own definition code that you want to insert into the framework. Therefore, the traditional proxy model is not feasible. XMPP modules and extensions need to be separated into their own separate classes. But each class needs to accept the proxy method. The standard nsnotification architecture will not be competent, and some proxies require a return variable. (plus it's really annoying to extract the parameters from the UserInfo dictionary notification).

So a multicastdelegate agrees that you use a standard proxy mode to insert the frame. However, it agrees that multiple classes accept proxy notifications that they want to use. The advantage of this is that you do not have to put all the XMPP processing code in a class. You can feel that your appropriate method is handled separately in different classes.

You can add/Remove Xmppstream proxy objects at any time:

[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];...[xmppStream removeDelegate:self];

A lot of other discussions about MulticastDelegate can be found here.

A more specific discussion of threads and queues can be found here.

Join Modules

This has some extensions about this framework, of course. You can write as many extensions as you wish.

We will not check all available extensions, but we will list a few examples here to demonstrate.

    • Xmppreconnect-Take the initiative to connect again. Suppose you accidentally disconnect.
    • Xmpproster-Support for standard XMPP nickname brochure
    • Xmpproom-Provides multiplayer chat support.
    • Xmpppubsub-Advertise your subscription

As an example, we will insert the Xmppreconnect module in our stream:

configurationoffortoAnd that‘sallisandto do its thing unless you deactivate it.
Connection

When you are ready, you are able to start the connection process:

NSError *errornil;if (![xmppStream connect:&error]){    NSLog(@"Oops, I probably forgot something: %@"error);}

Suppose you forget to set the required properties. such as Myjid, the Connect method returns no, and the error message notifies you of the problem.

During the connection process. Client and server undergo an XMPP handshake.

During this time, the server notifies the client of various protocols it supports and requires. Some servers may need to connect through SSL/TLS. Assuming this is the case, XMPP stream will proactively protect the connection, assuming you are connecting to the server to not X509 the certificate. You may need to implement the Xmppstream:willsecurewithsettings: Proxy method to change the default security settings.

Certification

After all the connection handshakes have ended Xmppstreamdidconnect: The proxy method is called. This is generally the most client should start the validation process:

- (void)xmppStreamDidConnect:(XMPPStream *)sender{    [xmppStream authenticateWithPassword:password error:NULL];}
XMPP Log

Here are a few goals for the entire XMPP schema log:

    • It must support multiple log levels.

      Not all log messages have the same priority. Some are about errors, and others are just information. Log ratings help developers ensure that their log information is complete. Able to open and close them without any difficulty whatsoever.

    • it must be configurable on a per-file basis

      A global log level will not be accepted when this framework contains so many files.

      Plus debugging issues often developers just want to see a few files in the log statements.

    • it must be configurable for end users

      The XMPP framework requires total control of the log statements, and there are many different requirements for the user. Some want the log to be declared into a file, some may want the log to be declared in the database. Or maybe they need to be declared in different places for the log, depending on whether the log claims originate in the app or the XMPP framework.

I have worked in the client for years. I see a third-party framework that has the same problem over and over again.

The third party libraries comes with the scattered nslog statements. Finally, users are required to stare at the NSLog statements through the library, or convert them to the version number of the original custom macro.

The XMPP framework uses a professional log framework, Cocoalumberjack.

This log frame is actually faster than the nslog. The same is true when doing the same thing. In addition, it supports a large number of different configurations and agrees that end users can even join their own definition of logging, filtering and formatting.

Here's what you need to know about the Xmppframework settings for the log:

For most files inside the frame you will find for example the following:

// Log levels: off, error, warn, info, verbosestaticconstint xmppLogLevel = XMPP_LOG_LEVEL_WARN;

As you can see there are 4 log levels (plus xmpp_log_level_none):

    • Error
    • Warning
    • Info
    • Verbose

You can change the log level regardless of the file. You want it to output a lot of other information.

In addition to this point. You can enable a trace flag. When tracing is enabled, it outputs the method being called.

Please note that. Traces are separated from the log level. For example, one can set the logging level to warning. and enable tracing like this:

// Log levels: off, error, warn, info, verbosestaticconstint xmppLogLevel = XMPP_LOG_LEVEL_WARN | XMPP_LOG_FLAG_TRACE;

In the case of code, it means:

XMPPLogTrace()// Enabled - Will spit out "<FileName>: <MethodName>"XMPPLogError(@"I will get logged");XMPPLogWarn(@"I will get logged");XMPPLogInfo(@"I will NOT get logged");XMPPLogVerbose(@"I will NOT get logged");

In addition, Xmppstream has an option that allows you to see the original XML sent/received being sent. You can put it in XMPPSTREAM.M like this:

// Log levels: off, error, warn, info, verbosestaticconstint xmppLogLevel = XMPP_LOG_LEVEL_INFO | XMPP_LOG_FLAG_SEND_RECV;

When you start the application. You need to configure the Lumberjack framework.

For those who have just started learning, you can simply set it up in your appdelegate:

#import "DDLog.h"#import "DDTTYLogger.h"- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{    [DDLog addLogger:[DDTTYLogger sharedInstance] withLogLevel:XMPP_LOG_FLAG_SEND_RECV];    // All your other code...}

A lot of other information about lumberjack here.
For more information on lumberjack take a look at its project page.

A lot of other information

Want to start right away?

    • Get started using xmppframework on IOS
    • Get started using xmppframework on Mac OS X

Expand your knowledge!

    • Learn about working with XML/XMPP elements

    • Learn about Xmppframework ' s threading architecture

IOS XMPP Framework Chinese Overview

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.

Tags Index: