smack

Getting Started With Smack

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

Requirements

The only requirement for Smack is JDK 1.2 or later 1. An XML parser is embedded in the smack.jar file and no other third party libraries are required.

1 JDK 1.2 and 1.3 users that wish to use SSL connections must have the JSSE library in their classpath.

Establishing a Connection

The XMPPConnection class is used to create a connection to an XMPP server. To create an SSL connection, use the SSLXMPPConnection class. Below are code examples for making a connection:

 

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

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

// Create an SSL connection to jabber.org.
XMPPConnection connection = new SSLXMPPConnection("jabber.org"); 

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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值