Smack的初始化涉及到两个步骤:
1.初始化系统属性――通过SmackConfiguration进行系统属性初始化。这些属性可以通过getxxx()方法获取。
2.初始化启动类――初始化类意味着在启动时候实例化该类,如果继承SmackInitializer则需要调用initialize()方法。如果不继承SmackInitializer则初始化的操作必须在静态代码块中,一旦加载类时自动执行。
Establishing a Connection创建连接
XmppTCPConnection类是被用来创建连接到xmpp服务器的。
// Create a connection to the jabber.org server._
XMPPConnection conn1 = new XMPPTCPConnection("jabber.org");
conn1.connect();
// Create a connection to the jabber.org server on a specific port._
ConnectionConfigurationconfig = new ConnectionConfiguration("jabber.org", 5222);
XMPPConnection conn2 = new XMPPTCPConnection(config);
conn2.connect();
ConectionConfiguration类提供一些控制操作,譬如是否加密。
Working with the Roster 花名册
Roster可以让你保持获取其他用户的presence状态,用户可以添加进组“Friend”或者“Co-workers”,你可以知晓用户是否在线。
检索roster可以通过XMPPConnection.getRoster()方法,roster类允许你查看所有的roster enteries ,群组信息当前的登录状态。
Reading and WritingPackets读写数据包
XMPP服务器和客户端间以XML传递的信息被称为数据包。
org.jivesoftware.smack.packet包内有三种封装好的基本的packet,分别是message, presence和IQ。
比如Chat和GroupChat类提供了更高级别的结构用以创建发送packet,当然你也可以直接用packet。
// 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 aXMPPConnection instance called "con").
con.sendPacket(presence);
Smack提供两种读取packets
PacketListener和PacketCollector,这两种都通过PacketFilter进行packet加工。
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.
(packet listener事件监听,而packet collector是一个packets的可以进行polling和blocking操作的结果集队列。)
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 listener一旦数据包传递抵达的时候你可以进行处理,packet collector则被使用在你需要等待一个指定的packet传递抵达时候。)
Packet collectors and listeners can be created using an Connection instance.
(packet listener和packet collector在connection实例中被创建。)
// Create a packet filter to listen for new messages from a particular
// user. We use an AndFilter to combine two other filters._
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class),
newFromContainsFilter("mary@jivesoftware.com"));
// Assume we've created a XMPPConnection name "connection".
// First, register a packet collector using the filter we created.
PacketCollectormyCollector = connection.createPacketCollector(filter);
// Normally, you'd do something with the collector, like wait for new packets.
// Next, create a packet listener. We use an anonymous inner class for brevity.
PacketListenermyListener = new PacketListener() {
public void processPacket(Packet packet) {
// Do something with the incoming packet here._
}
};
// Register the listener._
connection.addPacketListener(myListener, filter);
Managing Connection
Connect and disConnect
// Create the configuration for this new connection_
ConnectionConfigurationconfig = new ConnectionConfiguration("jabber.org", 5222);
AbstractXMPPConnection connection = new XMPPTCPConnection(config);
// Connect to the server_
connection.connect();
// Log into the server_
connection.login("username", "password", "SomeResource");
...
// Disconnect