当前位置: 首页 > 工具软件 > SMACK > 使用案例 >

Smack: Getting Started

公羊晟
2023-12-01

This document will introduce you to the Smack API and provide an overview of important classes and concepts.

JAR Files and Requirements

Smack is meant to be easily embedded into any existing JDK 1.5 or later Java application. It has no external dependencies (except for the Jingle voice chat functionality) and is optimized to be as small as possible. The library ships as several JAR files to provide more flexibility over which features applications require:

  • smack.jar -- provides core XMPP functionality and is the only required library. All XMPP features that are part of the XMPP RFCs are included.
  • smackx.jar -- support for many of the the extensions (XEPs) defined by the XMPP Standards Foundation, including multi-user chat, file transfer, user search, etc. The extensions are documented in the extensions manual.
  • smackx-debug.jar -- an enhanced GUI debugger for protocol traffic. It will automatically be used when found in the classpath and when debugging is enabled.

Establishing a Connection

The XMPPConnection class is used to create a connection to an XMPP server. Below are code examples for making a connection:

 

// Create a connection to the jabber.org server.
XMPPConnection conn1 = new XMPPConnection("jabber.org");
conn1.connect();

// Create a connection to the jabber.org server on a specific port.
ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);
XMPPConnection conn2 = new XMPPConnection(config);
conn2.connect();

Note that maximum security will be used when connecting to the server by default (and when possible), including use of TLS encryption. The ConnectionConfiguration class provides advanced control over the connection created, such as the ability to disable or require encryption. See Connection Management for full details.

Once you've created a connection, you should login using a username and password with the XMPPConnection.login(String username, String password) method. Once you've logged in, you can being chatting with other users by creating new Chat or GroupChat objects.

Working with the Roster

The roster lets you keep track of the availability (presence) of other users. Users can be organized into groups such as "Friends" and "Co-workers", and then you discover whether each user is online or offline.

Retrieve the roster using the XMPPConnection.getRoster() method. The roster class allows you to find all the roster entries, the groups they belong to, and the current presence status of each entry.

Reading and Writing Packets

Each message to the XMPP server from a client is called a packet and is sent as XML. The org.jivesoftware.smack.packet package contains classes that encapsulate the three different basic packet types allowed by XMPP (message, presence, and IQ). Classes such as Chat and GroupChat provide higher-level constructs that manage creating and sending packets automatically, but you can also create and send packets directly. Below is a code example for changing your presence to let people know you're unavailable and "out fishing":

 

// Create a new presence. Pass in false to indicate we're unavailable.
Presence presence = new Presence(Presence.Type.unavailable);
presence.setStatus("Gone fishing");
// Send the packet (assume we have a XMPPConnection instance called "con").
con.sendPacket(presence);

Smack provides two ways to read incoming packets: PacketListener, and PacketCollector. Both use PacketFilter instances to determine which packets should be processed. A packet listener is used for event style programming, while a packet collector has a result queue of packets that you can do polling and blocking operations on. So, a packet listener is useful when you want to take some action whenever a packet happens to come in, while a packet collector is useful when you want to wait for a specific packet to arrive. Packet collectors and listeners can be created using an XMPPConnection instance.

 类似资料:

相关阅读

相关文章

相关问答