Translation of Smack and smack

Source: Internet
Author: User

Translation of Smack and smack

Used in recent projectsOpenfire smack makes it easier for you to get familiar with and read more easily.For Smack 4.1.1 api documentation Translation

Smack document

Content:

  • Overview
  • Getting started
  • Connection Management
  • Message transmission
  • Roster and Presence
  • Process passed Stanzas
  • Provider Architecture
  • Smack debugging
  • Smack Extension

 

Overview

Smack is a communication Library used for real-time communication with XMPP servers, including instant messaging and group chat.

Main advantages of Smack

  • Simple, easy-to-use, and powerful API. sending messages to users can complete only a few lines of code:
AbstractXMPPConnection connection = new XMPPTCPConnection("mtucker", "password", "jabber.org");connection.connect().login();Chat chat = ChatManager.getInstanceFor(connection).createChat("jsmith@jivesoftware.com", new MessageListener() {    public void processMessage(Chat chat, Message message) {        System.out.println("Received message: " + message);    }});chat.sendMessage("Howdy!");
  • Unlike other class libraries that force you to perform package-level encoding, Smack provides more intelligent structural layers in the Chat and Roster classes to make your programming more effective.
  • You do not need to be familiar with xmpp xml format, even if you are familiar with XML.
  • It provides a simple design for communication. Smack can set any number of attributes in each message, including attributes of Java objects.
  • Open-source Class Libraries under Apache license, which means you can use Smack to your commercial or non-commercial applications.

About XMPP

XMPP (eXtensible Messaging and Presence Protocol) is an open XML-based Protocol led by the Jabber Software Foundation (http://www.xmpp.org) and is approved by IETF.

 How to use this document

This document assumes that you are familiar with the main features of XMPP instant messaging. It is also strongly recommended that you open the Javadoc API and guide the use of these documents as a reference.

 

Getting started

This document will introduce Smack APIs and briefly introduce some important classes and concepts.

JAR files and requirements

Smack is easily embedded into any existing Java application. Only a few JAR packages in this class library provide more powerful flexibility for applications:

  • Smack-core.jar-provides core XMPP functionality. All XMPP features include part of xmpp rfcs.
  • Smack-tcp.jar -- for XMPPSupportedTCP. IncludingYou usually want to useXMPPTCPConnection class.
  • Smack-extensions.jar-supports many extensions (XEPs) based on the XMPP Standard definition, including multi-user chat, file transfer, user search and so on. These extended backups are in the extended manual.
  • Smack-experimental.jar-based on the standard definition of XMPPSupport for experiment extension (XEPs). These extended APIs and functions should be considered unstable.
  • Smack-legacy.jar --The standard definition of XMPP supports legacy extensions (XEPs ).
  • Smack-bosh.jar -- Support for BOSH (XEP-0124. This programming should be defined as a test version.
  • Smack-jingle.jar-Support for Jingle. However, maintenance is not frequently performed so far.
  • Smack-resolver-dnsjava.jar-dns srv records can be resolved with the help of dnsjava. The ideal platform does not support javax.
  • Smack-resolver-javax.jar-works with the javax namespace API to support resolution of dns srv records
  • Smack-debug.jar-a graphical interfaceOfEnhanced debugger. It is automatically enabled when the class is enabled.

Configuration

Smack has an initialization process that involves two phases.

  • Initialize system properties-all system properties can be initialized through the SmackConfiguration class. These attributes can be obtained through getxxx () in the class.
  • Initialize the startup class -- initialize any class through the instance class, and then call the initialize method if it inherits to the SmackInitializer class. If it does not inherit this interface, initialization must occur in a static method and is automatically executed when the class is loaded.

Initialization is completed through a configuration file. By default, Smack is loaded on org. jivesoftware. Smack/smack-config. xml in smack. jar. This special structure contains an initialization class list for loading. All the Manager classes to be initialized are included in the initialization class list.

Establish a connection

XMPPConnectionClass is used to establish a connection to the XMPP server. The following is an example of establishing a connection:

// Create a connection conn1 = new XMPPTCPConnection ("username", "password" "jabber.org") for the jabber.org server; conn1.connect (); // create a connection XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration for the jabber.org server on a specific port. builder (). setUsernameAndPassword ("username", "password "). setServiceName ("jabber.org "). setHost ("earl.jabber.org "). setPort ("8222 "). build (); AbstractXMPPConnection conn2 = new XMPPTCPConnection (config); conn2.connect ();

Note that the safest way to connect to a server is to use TLS encryption for default connections (if possible. The ConnectionConfiguration class provides advanced management to create connections, such as enabling and disabling encryption, and observes the complete management process of XMPPConnection.

Once you create a connection, you must log on with the XMPPConnection. login () method. After you log on, you can create chat and chatGroup objects to chat with other users.

Operate Roster

Roster allows you to track the validity (existence) of other users ). Users can organize users by using groups such as "friends" and "colleagues" to check whether each user is online.

UseRoster.getInstanceFor(XMPPConnection)This method gets Roster. With the Roster class, you can find all Roster entries, their groups, and the current status of each entry.

Read and Write Stanzas

Each message sent from the client to the XMPP server in XML format is called a "packet ".Org. jivesoftware. smack. packetThe package contains classes that encapsulate three different basic packet types (message, presence, and IQ) allowed by XMPP ). Classes such as Chat and GroupChat provide a higher category structure to automatically create and send packages, but you can also directly create and send packages. The following is an example of changing your presence to let others know that you are no longer valid. It is "out fishing:

// Create a new presence. false is passed to indicate that we have invalid Presence presence = new Presence (Presence. type. unavailable); presence. setStatus ("Gone fishing"); // send packet (assuming there is an XMPPConnection instance named "con ). con. sendStanza (presence );

Smack provides two methods to read the received packet:PacketListener [packet listener]AndPacketCollector[Packet collector]. Both use the StanzaFilter instance to determine which packet should be processed. The packet listener is used for event style programming, while the packet collector has a packet result queue that can perform polling and blocking operations. Therefore, when you want to take some operations on a packet that may arrive at any time, use the packet listener. When you want to wait for a special packet to arrive, use the packet collector. You can use the XMPPConnection instance to create a packet collector and listener.

 

It's a little late, and the rest of the two days are being completed.

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.