I am sorry that the address of swift is attached: github.com/tanzhiwen/S…
XMPP explanation (http://www.cnblogs.com/jiyuqi/tag/xmpp/)
XMPP (eXtensible Messaging and Presence Protocol) is a Protocol for delivering small structured data between two locations. On this basis, XMPP has been used to build large-scale instant messaging systems, gaming platforms, collaboration Spaces, and voice and video conferencing systems.
XMPP is made up of several small building blocks, which are expanded to include more building blocks. There are many systems in XMPP: publish-subscribe services, multiplayer chat, form retrieval and processing, service discovery, real-time data transfer, privacy handling, and remote procedure calls.
Most social media (Facebook and Twitter) also use the XMPP protocol.
What is a XMPP
Like other protocols, XMPP defines the format in which data is passed between two or more communicating entities. In the case of XMPP, entities usually refer to client servers, but they also allow client-to-client or server-side communication.
XML data is exchanged on XMPP, and this format makes the XMPP protocol extremely extensible because it is easy to add new features and ensure forward and backward compatibility using XML. Using XML takes up more bandwidth than a binary protocol, but has the advantage of nearly unlimited scalability.
Users can register extensions to the XMPP Standards Foundation protocol.
In XMPP, XML data is organized as a stream, with each stream corresponding to one direction of communication. Each XML stream consists of a start element, followed by an XMPP section and other top-level elements, followed by an end element. Each XMPP section, which can have child elements and attributes, is a first-level child of the stream. At the end of the XMPP connection, the two streams form a valid pair of XMPP documents.
XMPP sections form the core of the protocol, and XMPP applications focus on how to send and respond to various types of sections. Sections may contain information about other entities on the network, personal messages similar to E-mail, or structured data designed for computer processing. In a typical XMPP session, a section described above will be sent from Darcy’s XMPP client to her XMPP server, and her server will notice that the section’s destination is an entity on some remote server, Therefore, an XMPP connection is established with the remote server and messages are forwarded there.
This communication network is similar to E-mail, but unlike E-mail servers, servers between XMPPS can communicate directly without the need for an intermediate server.
This direct communication avoids the interference of spam messages and supports Transport Layer Security (TLS) to encrypt communication and Simple Authentication and Security Layers (SASL). Simple Authentication and security layer) implements the authentication mechanism.
XMPP is designed to pass small messages rather than large chunks of data, but it can be used to negotiate and establish in-band or out-of-band transport that can pass large chunks of data between endpoints.
XMPP network
Any XMPP network is made up of several roles, which can be categorized as server side, client, component, and server plug-in.
Unlike the WWW and EMAIL networks, XMPP networks hop between XMPP servers only once, whereas the EMAIL protocol has multiple relay servers, and XMPP keeps a complete list.
The server
The XMPP server is the communication system for any XMPP network, and the job of the server is to provide routing for XMPP sections. Whether the sections are sent internally from one user to another or from a local user to the server.
A group of XMPP servers that can communicate with each other constitutes an XMPP network.
XMPP servers always allow users to connect to themselves, but you can also write applications and programs that use the server-server protocol directly to reduce routing costs
Ejabberd, Openfire, and Tigase are three open source servers that run on Windows, Mac OS X, and Linux.
M-link and Jabber XCP are commercial products.
The client
Most XMPP entities are clients that connect to an XMPP server through the client-server protocol.
The client must authenticate to an XMPP server somewhere. The server routes all sections sent by the client to the appropriate destination.
The server is also responsible for managing several other aspects of the client session, including the roster and the raw address.
component
Not only can clients connect to XMPP servers, but most servers also support external server components. These components enhance the behavior of the server by adding some kind of new service. These components have their own identities and addresses on the server, but run externally and communicate through component protocols.
The component protocol (XEP-0114) allows developers to create server extensions, such as multiplayer chat services, in a way that the server is not aware of.
Components also need to authenticate to the XMPP server, but it is simpler than full SASL authentication on the client side, such as a password.
Each component programs a individually addressable entity inside the server that looks like a child server from the outside. The XMPP server does not manage routing for any section other than the base section in place of the connected component.
The server is also more flexible by allowing components to route or manage sections internally.
The plug-in
Many XMPP servers also support extension using plug-ins, but plug-ins are deep inside the server and have high efficiency and minimal versatility.
Plug-ins are typically bound to a particular type of server.
XMPP addressing
Each entity on an XMPP network has one or more addresses (called Jids, Jabber identifiers). Usually something like:
[email protected] and [email protected] are the two Jids.
The JID consists of three parts, the node, the domain, and the resource. The domain is required and the other two parts are optional.
A domain is a resolvable DNS name for an entity (server, component, or plug-in). The domain-only JID is a valid address and represents the server address. Sections pointing to the domain will be handled by the server itself and may be routed to a component or plug-in.
The local part is usually used to identify a specific user in the domain and is preceded by @. The local part can also be used to identify other objects, such as a chat room.
The resource portion of a JID typically identifies an XMPP connection for a particular client. For XMPP clients, each connection is assigned a resource. For example, [email protected] can connect his calligraphy to the library
[email protected]/study and [email protected]/library, which prevents messages from finding the correct handler when the user opens multiple links. The main thing to notice is that the resources section is case sensitive.
Jids are divided into two types:
bare JID
The full JID removes the address of the resource part, and the client’s raw JID is somewhat special because the server itself processes the raw JID section sent to the client. A bare JID can be treated as the account of the addressing user, not the client.
Complete JID
The most specific address
XMPP section The core XMPP toolset consists of three basic sections, namely, and, and the XMPP stream consists of two XML documents, one for each direction of communication, which have a root element, and the child elements of which are composed of routable sections and the top-level child elements associated with the stream. Copy code // request your own roster // notify server she is online and accessible I cannot talk of books in a ball-room; my head is always full of something else.
The properties from/to/type/ idFROM are not provided by the client, but are marked by the server. Presence A presence provides access to network entities. Users presence, showed his online, so I can have greater probability to communicate with others, people are more willing to communicate with people online), but we don’t have to worry about anyone can also see your online status, unless we have subscribed to the user’s state, subscription, the user’s status information will be automatically sent to the subscriber. In fact, XMPP’s Presence section is a simple, dedicated publish-subscribe method. In IM, presence is represented in the roster, which holds the list of jids and the user’s subscriptions to those Jids. Once online, the user sends the Presence section, and the server takes care of the rest (notifies oneself that they are online, The message section is used to send messages from one entity to another, and can transmit any type of structured information without ensuring transmission reliability. Message is a very basic push model. Message is commonly used for IM, groupchat, warnings and notifications, etc. There are several types of message: Normal is similar to email, which is sent without waiting for a response. Chat Is used for real-time communication between two entities. Groupchat Query In addition to type, a typical Message section also contains a FROM, to, or ID attribute (for destination tracing). The JID in to is the receiver of the message, and from is the JID of the sender, but the FROM attribute is not provided by the client, but by the sender’s server to avoid address impersonation. The Message section can also contain loads that are not defined in the XMPP protocol and can be used for extension. The IQ section represents Info/Query and provides a request and response mechanism for XMPP communication, similar to the GET/POST/PUT methods. The IQ can contain only one payload and defines requests or actions that need to be handled by the server. IQ is more reliable than Message because it requires a response. The IQ contains the ID attribute, which identifies the response sent back by the server. Get is used to request information, similar to HTTP Getset to provide information or requests, similar to HTTP POST/PUTresult to respond to requests, Similar to HTTP 200Error error message example send get roster request server return roster copy code Copy code User add a contact the server responds to the error section with a clear structure, usually containing the original section content, The extensible XMPP protocol is xmL-based and therefore inherently extensible. We can use XMPP to pass all kinds of information, including links, location information, Web services, and so on. Connection life Cycle Sending XMPP sections typically requires the establishment of an authenticated XMPP session, including connection, flow establishment, authentication, and disconnection. Connection Before any stanza can be sent, the XMPP stream needs to be established, and a connection to the XMPP server must be established before the XMPP stream exists. When an XMPP client or server connects to another XMPP server, the SRV record is first queried, which holds a list of servers for a specific domain. The query reply can contain multiple SRV records so that load-balancing connections can be established across multiple servers. If no suitable SRV record is found, the program attempts to connect directly to the specified domain. Establishment of the stream Once the connection is established through the given XMPP server, the XMPP stream is started send to the server, you can open the XMPP stream, the server sends the start flag of the response stream to respond after the establishment of the XMPP stream various elements can be sent back and forth the server sends elements, listing all functions supported in the XMPP stream, Most authentication related to available encryption and authentication selection XMPP allows Transport Layer Security (Transport Layer Security) encryption, and most clients use it by default. Once the server advertises TLS support, the client initiates the TLS connection and upgrades the current socket to an encrypted socket to keep the connection open. Once TLS encryption is established, a new pair of XMPP streams are created. Authentication in XMPP uses the Simple Authentication and Security Layers (SASL) protocol and supports multiple Authentication mechanisms (depending on the server). Once authenticated, the client must bind a resource to the connection and start a session, which is sent through the and elements. When two servers are connected to each other, authentication steps are slightly different. Disconnect When a user terminates an XMPP session, they terminate the session and disconnect, the most elegant way to do so is to first send an invalid attendance message and then close the element.