Brief analysis of the use of XMPP framework and IOS-OBJECTIVE-C

Source: Internet
Author: User
Tags uikit

This two days look at the XMPP framework, read some information, write down this article to record the study notes

I. Brief analysis of the core of XMPP

The XMPP framework is divided into two parts

1. Core Components

2. Extension section

The extended section focuses on the buddy list (roster), auto-automatic reconnect, and a few other implementations.

The core section includes the following sections:

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

1.XMPPStream

The Xmppstream class is equivalent to an input-output stream used to connect to the server and send messages.

Xmppstream Add and remove agents write a method

1 -(void) AddDelegate: (ID)delegate  delegatequeue: (dispatch_queue_t) Delegatequeue; 2 -(void) Removedelegate: (ID)delegate  delegatequeue: (dispatch_queue_t) Delegatequeue; 3 -(void) Removedelegate: (ID)delegate;

The XMPP bottom is the socket, so xmppstream implements the socket protocol <gcdasyncsocketdelegate>

Initialize method

-(ID) init{    if (self = [super init]))    {        //  Common Initialization        [self commoninit];                 // Initialize Socket        Asyncsocket = [[Gcdasyncsocket alloc] initwithdelegate:self delegatequeue:xmppqueue];    }     return Self ;}

Xmppstream contains some of the properties

/** Jabber ID is used to represent the address of the user's identity */*myjid;

/** the domain name of the server you want to connect to IS */
@property (readwritecopynsstring  *hostname;
/** the port number of the server to be connected defaults to 5222*/
@property (readwriteassignUInt16 hostport;

2.XMPPParser

Xmppparser, is the parser of Xmppstream, parsing the information passed

3.XMPPJID

Represents an address in the XMPP protocol, consisting of the following three parts

Node/username: Represents an Entity (node, user name, user's basic identity) to a server or gateway and uses a network service

Domain: Represents a gateway or server in a network (for example, a jid,[email Protected]/resource,domian that is followed by a domain name)

Resource: Represents a specific reply (or a device), connection (or address), or an entity object (or a participant in a multiplayer chat room) that is associated with a Node ID entity, which can be used to differentiate the user's device, etc.

There are other properties defined,

Bare: That is node+domain,[email protected], Jid remove resource

Full: A complete Jid, containing username,domain,resource, more than bare resource

Xmppjid source of an enumeration, look at the numbers and, well understood

enum xmppjidcompareoptions{    xmppjidcompareuser     1//001    Xmppjidcomparedomain   2//010    4 //         xmppjidcomparebare     3//011    xmppjidcomparefull     7  //111};

Xmppjid use of documentation, archiving, compliance <nscoding, nscopying> Protocol

4.XMPPElement

Xmppelement is a base class of 3 basic elements (Iq,message,presence)

Inherit from Nsxmlelement

Use with classification nsxmlelement+xmpp to make your code more concise and more readable

5.XMPPIQ

Request

The primary property is the type (message and presence, which represent the request or the kind of message)

<type= "Result"
 from = "[Email protected]/contact"
to="google.com"
= ' 123456 ' >
<query xmins= "Jabber:iq:roster"/>
<iq/>

Type attribute: Indicates that the IQ type is get, like server-side request information

From property: careful source, =jid

To property: Message destination, = Server domain name

The id attribute, optionally , marks the request ID, and when the server finishes processing the request get type IQ, the response result type IQ and ID are the same as the ID of the request IQ

6.Message

<message/> section defines the message semantics, <message/> section can be regarded as a "push" mechanism, similar to the communication that occurs in the email system. All message sections should have a ' to ' attribute that specifies the intended recipient of the message

The message is used to "forget after sending" (after sending does not verify whether the message received successfully), such transmission is mainly applied with human readable text, warning, notification and other information.

<to= "[Email protected]/contact"     type= "chat">     <body>        Hello    <  body  /><meesage/>

7.Presence

<from= "">    <show> content displayed  <show/>    <status> State <  status/><presence/>

The State of Presence

Available online

Away leave

Do not Disturb busy

Unavailable downline

Ii. introduction of the XMPP framework

The steps described by XMPP on Github-wiki have expired

But the connection to the stack flow answer is given.

http://stackoverflow.com/questions/9091767/up-to-date-instructions-on-how-to-install-xmppframework-manually/ 30543948#30543948

Step 1

You must import the Xcode folder

    • Vendor/cocoaasyncsocket
    • Vendor/cocoalumberjack
    • Vendor/kissxml
    • Vendor/libidn
    • Authentication
    • Categories
    • Core
    • Utilities

You can also import extensions, optional

When importing, choose to copy into the project

Step 2

Import header file XMPPFramework.h, also to assign a value option

Frames that are finished importing

Step 3

To import the following self-brought frames and libraries

    • Cfnetwork.framework
    • Security.framework
    • Libxml2.dylib
    • Libresolv.dylib
    • Libidn.a

Note that the Dylib suffix name of the Xcode9 library after the project Xcode8 is changed to THD and may be re-imported

Step 4

Build settings to add

OTHER linker flags =-LXML2

HEADER SEARCH PATHS =/usr/include/libxml2

Pay attention to the final import <UIKit/UIkit.h>

Iii. brief use of the XMPP framework

Here you omit the configuration server (with the OpenFire) and the database (MySQL) steps, many tutorials on the web

Step 1 Connect the server

-(void) Connect {
   if (Self.xmppstream = = nil) { self.xmppstream = [[Xmppstream alloc] init]; [Self.xmppstream adddelegate:self Delegatequeue:dispatch_get_main_queue ()]; }
  //Set Jid and then use Xmppstream to connect to the server if (![ Self.xmppstream isconnected]) { NSString *username = [[Nsuserdefaults standarduserdefaults] objectForKey:@ " Username "]; Xmppjid *jid = [Xmppjid jidwithuser:username domain:@ "Lizhen" resource:@ "Ework"]; [Self.xmppstream Setmyjid:jid]; [Self.xmppstream sethostname:@ "10.4.125.113"]; Nserror *error = nil; if (![ Self.xmppstream Connect:&error] { NSLog (@ "Connect error:%@", [[Error UserInfo] Description]); }}}

Connection successful regretting method of calling Xmppstreamdelegate

The first call is

-(void) Xmppstream: (Xmppstream *) sender Socketdidconnect: (Gcdasyncsocket *) socket

It then calls

-(void) Xmppstreamdidconnect: (Xmppstream *) sender

Step 2

After successful connection, send password authorization

-(void) sendpwdtohost{    nserror *err = nil;     Ybuserinfo is a custom data type used to store user names, passwords, etc.    //from a singleton to obtain a password    nsstring *pwd = [Ybuserinfo sharedwcuserinfo].pwd;    //Use Xmppstream to send password    [_xmppstream authenticatewithpassword:pwd error:&err];        if (err) {          NSLog (@ "%@", err);}    }

Step 3

After authorization is successful, send a message

-(void) sendonlinetohost{        xmpppresence *presence = [xmpppresence presence];        [_xmppstream sendelement:presence];    }

Disconnecting from the server invokes the proxy method

-(void) Xmppstreamdiddisconnect: (Xmppstream *) sender Witherror: (Nserror *) error

Authorization succeeds calling method

-(void) Xmppstreamdidauthenticate: (Xmppstream *) sender

  

Authorization Failures Call methods

-(void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (ddxmlelement *) error

  

The method is called when the registration succeeds

-(void) Xmppstreamdidregister: (Xmppstream *) sender

  

Registration failure Calls method

-(void) Xmppstream: (Xmppstream *) sender Didnotregister: (ddxmlelement *) error

  

Exit and Disconnect

-(void) Disconnect {    xmpppresence *presence = [xmpppresence presencewithtype:@ "unavailable"];    [Self.xmppstream sendelement:presence];          [Self.xmppstream disconnect];}

  

The purpose of writing this blog is to deepen your understanding of XMPP.

Looked up some information, many are all English, still want to learn English well

Reprint please indicate the source

Brief analysis of the use of XMPP framework and IOS-OBJECTIVE-C

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.