Smack document (translated)

Source: Internet
Author: User

Recent projects to use the openfire smack to make yourself faster familiar with others reading convenience , for Smack 4.1.1 API document translation

Smack Documentation

Content:

    • Overview
    • Getting Started Guide
    • Connection Management
    • Message delivery
    • roster and Presence
    • Handling Incoming Stanzas
    • Provider Architecture
    • Smack Commissioning
    • Smack extension

Overview

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

the main advantages of smack

    • Simple, easy-to-use and powerful API. Sending a message to the user can be done in just a few lines of code:
New Xmpptcpconnection ("Mtucker", "Password", "jabber.org"new  MessageListener () {      publicvoid  processmessage (chat chat, message message) {        System.out.println ("Received Message: "+ message);}    }); Chat.sendmessage ("howdy!");
    • Unlike other class libraries, you are forced to encode at the package level. Smack provides a more intelligent hierarchy of structures in the chat and roster classes, making your programming more efficient.
    • You do not need to familiarize yourself with the XMPP XML format, even if you are familiar with XML.
    • Provides a simple design for communication, smack can set any number of attributes in each message, including the properties of the Java object.
    • An open source class library under Apache licensing, which means you can use smack in your commercial or non-commercial applications.

about XMPP

XMPP (extensible Messaging and Presence Protocol) is an open XML-based protocol led by Jabber Software Foundation (http://www.xmpp.org), and obtained the approval of the IETF.

How to use the document

This document assumes that you are already 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 when reading them.

Getting Started Guide

This document will introduce you to the smack API and briefly introduce some important classes and concepts.

Jar Files and Requirements

Smack is very easy to embed into any existing Java application. Only a few jar packages in this class library provide more flexibility for the application:

  • smack-core.jar  --  提供核心XMPP功能。所有XMPP特性包括XMPP RFCS的一部分。
  • smack-tcp.jar  --  对XMPP支持TCP。包括你通常想要使用XMPPTCPConnection类。
  • smack-extensions.jar  --  在XMPP标准定义的基础上支持许多扩展(XEPs),包括多用户聊天,文件传输,用户搜索等等。这些扩展备份在扩展手册中。
  • smack-experimental.jar  --  在XMPP标准定义的基础上支持实验扩展(XEPs)。这些扩展的API和功能应该被认为是不稳定的。
  • smack-legacy.jar  --  在XMPP标准定义的定义上支持遗留扩展(XEPs)。
  • smack-bosh.jar  --  对BOSH (XEP-0124)的支持。这样编程应该被定义为测试版。
  • smack-jingle.jar  --  对Jingle支持。但到目前为止经常没有维护。
  • smack-resolver-dnsjava.jar  --  在dnsjava的帮助下支持解析DNS SRV记录。理想的平台,不支持javax。
  • smack-resolver-javax.jar  --  与javax命名空间API一起支持解决DNS SRV记录
  • smack-debug.jar  --  一个有图形界面的增强型调试器。它会在类中允许启动时自动开启。

Configuration

Smack has an initialization process that involves 2 phases.

    • Initialize System Properties--all system properties can be initialized with the class smackconfiguration. These properties can be obtained in the class through the getxxx ()
    • Initializes the startup class--Initializes any class to pass the instance class, and then calls the Initialize method if it inherits to the Smackinitializer class. If it does not inherit this interface, then initialization must occur in a static method, which is executed automatically when the class load is complete.

Initialization is done through a configuration file. By default, Smack is loaded on org.jivesoftware.smack/smack-config.xml in Smack.jar. This particular structure contains a list of initialization classes to load. All of the manager classes that need to be initialized are included in this initialization class list.

Establish a connection

The xmppconnection class is used to establish a connection to the XMPP server. Here is an example of establishing a connection:

 //  Create a connection for the jabber.org server  Abstractxmppconnection conn1 = new  xmpptcpconnection ("username", "password" " jabber.org " //  xmpptcpconnectionconfiguration config = Xmpptcpconnectionconfiguration.builder (). Setusernameandpassword ( "username", "password" ). Setservicename ( " jabber.org " "earl.jabber.org" ). Setport ( "8222"  = new   

Note that the safest way to connect to a server is to include the use of TLS encryption, if possible, in the default connection. The Connectionconfiguration class provides advanced management to create connections, such as opening and disabling encryption capabilities, and observing the xmppconnection complete management process.

Once you have created a connection, you must log in with the Xmppconnection.login () method. Once you're signed in, you can chat with other uses by creating chat and Chatgroup objects.

Operation Roster

Roster allows you to track the validity (presence) of other users. Users can organize users by using groups such as "Friends" and "colleagues" to see if each user is online.

Roster.getInstanceFor(XMPPConnection)get roster by using this method. With the roster class you can find all roster entries, the groups they belong to, and the current presence status of each entry.

Read/write Stanzas

Each message sent from the client to the XMPP server in XML format is called a "packet". The Org.jivesoftware.smack.packet package contains classes that encapsulate the three different basic packet types allowed by XMPP (message, presence, and IQ). Classes like chat and Groupchat provide a higher class of constructs that can automatically create and send packet, but you can also create and send packet directly. Here's a "out fishing" by changing your presence to let others know that you are invalid:

// create a new presence. Pass in false to indicate that we are invalid . New Presence (Presence.Type.unavailable);p resence.setstatus ("Gone fishing"); // Send packet (assuming you already have a xmppconnection instance named "Con"). Con.sendstanza (presence);

Smack provides two ways to read received packet:Packetlistener[packet Listener] and packetcollector[packet collector]. Both use the Stanzafilter instance to determine which packet should be processed. The packet listener is used for programming event styles, while the packet collector has a result queue that can do polling and blocking operations for packet. So, when you want to take some action on a packet that is likely to come, use the packet listener, and when you want to wait for a special packet to arrive, use the packet collector. You can use Xmppconnection instances to create packet collectors and listeners.

A little late, the rest of the two days in the completion of the

Smack document (translated)

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.