XMPP Client library Smack development of 4.0.6 Version III
Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs
Vi. examples of establishing connections
The Xmppconnection class is used to create a connection to the XMPP server, with the following code examples:
Create a connection to the jabber.org server abstractxmppconnection conn1 = new Xmpptcpconnection ("username", "password", "jabber.org"); Conn1.connect ();
Create a connection to the specified port on the jabber.org server xmpptcpconnectionconfiguration config = Xmpptcpconnectionconfiguration.builder (). Setusernameandpassword ("username", "password"). Setservicename ("jabber.org"). Sethost ("earl.jabber.org"). Setport ( "8222"). Build (); Abstractxmppconnection conn2 = new xmpptcpconnection (config); Conn2.connect ();
Note that when you connect to an XMPP server, the default settings are used to maximize security, including applications for TLS encryption. The Connectionconfiguration class has advanced control over the connections created, such as the ability to turn encryption on or off.
You can view the document "Xmppconnection Management" later:
Https://github.com/igniterealtime/Smack/blob/master/documentation/connections.html
(Currently invalid)
Once you have created a connection, you should call the Xmppconnection.login () method to log on to the server. Once you're signed in, you can start chatting with other users by creating a Chat object or Groupchat object.
Vii. use of Roster (list)
Roster is used to track whether other users are online. Users ' contacts can be organized in groups, such as "Friends", "colleagues". You can then see if each user in the group is online.
To retrieve roster, use the Xmppconnection.getroster () method. The Roster class allows you to find all the roster entities, and which group they belong to, and the current online status of each entity.
Viii. reading and writing packet (packet)
Each message sent from the client to the XMPP server is called a packet (packet). The Org.jivesoftware.smack.packet library contains encapsulated classes of three different basic packet types supported by XMPP (message messages, online status presence, IQ). Classes like chat or groupchat provide a higher-level structure to manage the automatic creation and delivery of packets. However, developers can also create and send packets directly.
The following code is to modify your online status so that others know you are not online.
Creates a new online status object and sets the offline status presence presence = new Presence (Presence.Type.unavailable);p resence.setstatus ("Gone fishing"); /Send packet (assuming we already have Xmppconnection connection instance concon.sendpacket (presence);
Smack provides two ways to read incoming packets: Packetlistener (Packet listener) and Packetcollector (packet collector).
Both use the Packetfilter instance to determine which packet should be processed.
Packetlistener (packet listener) is used for event-style programming, while the Packetcollector (packet collector) has a result queue for a packet, and you can do the polling or blocking operations.
That is, if you want to perform some action when the packet arrives, the packet listener is a good fit. If you want to wait for the specified packet to arrive, then the package collector is a good fit.
Both the package collector and the package listener are created using the connection connection instance.
XMPP Client library Smack development of 4.0.6 Version III