public interface XMPPConnection
XMPPTCPConnection
or XMPPBOSHConnection
). To create a connection to a XMPP server
a simple usage of this API might look like the following:
// Create a connection to the igniterealtime.org XMPP server. XMPPConnection con = new XMPPTCPConnection("igniterealtime.org"); // Connect to the server con.connect(); // Most servers require you to login before performing other tasks. con.login("jsmith", "mypass"); // Start a new conversation with John Doe and send him a message. Chat chat = ChatManager.getInstanceFor(con).createChat("jdoe@igniterealtime.org", new MessageListener() { public void processMessage(Chat chat, Message message) { // Print out any messages we get back to standard out. System.out.println("Received message: " + message); } }); chat.sendMessage("Howdy!"); // Disconnect from the server con.disconnect();
Connections can be reused between connections. This means that an Connection may be connected, disconnected and then connected again. Listeners of the Connection will be retained across connections.
Modifier and Type | Interface and Description |
---|---|
static class |
XMPPConnection.FromMode |
Modifier and Type | Method and Description |
---|---|
void |
addConnectionListener(ConnectionListener connectionListener)
Adds a connection listener to this connection that will be notified when
the connection closes or fails.
|
void |
addPacketInterceptor(PacketInterceptor packetInterceptor,
PacketFilter packetFilter)
Registers a packet interceptor with this connection.
|
void |
addPacketListener(PacketListener packetListener,
PacketFilter packetFilter)
Registers a packet listener with this connection.
|
void |
addPacketSendingListener(PacketListener packetListener,
PacketFilter packetFilter)
Registers a packet listener with this connection.
|
PacketCollector |
createPacketCollector(PacketFilter packetFilter)
Creates a new packet collector for this connection.
|
PacketCollector |
createPacketCollectorAndSend(IQ packet)
Creates a new packet collector collecting packets that are replies to
packet . |
int |
getConnectionCounter()
Get the connection counter of this XMPPConnection instance.
|
java.lang.String |
getConnectionID()
Returns the connection ID for this connection, which is the value set by the server
when opening a XMPP stream.
|
XMPPConnection.FromMode |
getFromMode()
Get the currently active FromMode.
|
java.lang.String |
getHost()
Returns the host name of the server where the XMPP server is running.
|
long |
getPacketReplyTimeout()
Returns the current value of the reply timeout in milliseconds for request for this
XMPPConnection instance.
|
int |
getPort()
Returns the port number of the XMPP server for this connection.
|
Roster |
getRoster()
Returns the roster for the user.
|
RosterStore |
getRosterStore()
Get the permanent roster store.
|
java.lang.String |
getServiceCapsNode()
Retrieve the servers Entity Caps node
XMPPConnection holds this information in order to avoid a dependency to
smackx where EntityCapsManager lives from smack.
|
java.lang.String |
getServiceName()
Returns the name of the service provided by the XMPP server for this connection.
|
java.lang.String |
getUser()
Returns the full XMPP address of the user that is logged in to the connection or
null if not logged in yet.
|
boolean |
isAnonymous()
Returns true if currently authenticated anonymously.
|
boolean |
isAuthenticated()
Returns true if currently authenticated by successfully calling the login method.
|
boolean |
isConnected()
Returns true if currently connected to the XMPP server.
|
boolean |
isRosterLoadedAtLogin()
Returns true if the roster will be loaded from the server when logging in.
|
boolean |
isRosterVersioningSupported()
Returns true if the server supports roster versioning as defined in XEP-0237.
|
boolean |
isSecureConnection()
Returns true if the connection to the server has successfully negotiated encryption.
|
boolean |
isUsingCompression()
Returns true if network traffic is being compressed.
|
void |
removeConnectionListener(ConnectionListener connectionListener)
Removes a connection listener from this connection.
|
void |
removePacketCollector(PacketCollector collector)
Remove a packet collector of this connection.
|
void |
removePacketInterceptor(PacketInterceptor packetInterceptor)
Removes a packet interceptor.
|
void |
removePacketListener(PacketListener packetListener)
Removes a packet listener for received packets from this connection.
|
void |
removePacketSendingListener(PacketListener packetListener)
Removes a packet listener for sending packets from this connection.
|
void |
sendPacket(Packet packet)
Sends the specified packet to the server.
|
void |
setFromMode(XMPPConnection.FromMode fromMode)
Set the FromMode for this connection instance.
|
void |
setPacketReplyTimeout(long timeout)
Set the packet reply timeout in milliseconds.
|
java.lang.String getServiceName()
java.lang.String getHost()
int getPort()
java.lang.String getUser()
java.lang.String getConnectionID()
boolean isConnected()
boolean isAuthenticated()
boolean isAnonymous()
boolean isSecureConnection()
boolean isUsingCompression()
void sendPacket(Packet packet) throws SmackException.NotConnectedException
packet
- the packet to send.SmackException.NotConnectedException
Roster getRoster()
This method will never return null
, instead if the user has not yet logged into
the server or is logged in anonymously all modifying methods of the returned roster object
like Roster.createEntry(String, String, String[])
,
Roster.removeEntry(RosterEntry)
, etc. except adding or removing
RosterListener
s will throw an IllegalStateException.
java.lang.IllegalStateException
- if the connection is anonymousvoid addConnectionListener(ConnectionListener connectionListener)
connectionListener
- a connection listener.void removeConnectionListener(ConnectionListener connectionListener)
connectionListener
- a connection listener.PacketCollector createPacketCollectorAndSend(IQ packet) throws SmackException.NotConnectedException
packet
.
Does also send packet
. The packet filter for the collector is an
IQReplyFilter
, guaranteeing that packet id and JID in the 'from' address have
expected values.packet
- the packet to filter responses fromSmackException.NotConnectedException
PacketCollector createPacketCollector(PacketFilter packetFilter)
PacketListener
when you need to wait for
a specific result.packetFilter
- the packet filter to use.void removePacketCollector(PacketCollector collector)
collector
- a packet collectors which was created for this connection.void addPacketListener(PacketListener packetListener, PacketFilter packetFilter)
addPacketInterceptor(PacketInterceptor, PacketFilter)
.packetListener
- the packet listener to notify of new received packets.packetFilter
- the packet filter to use.void removePacketListener(PacketListener packetListener)
packetListener
- the packet listener to remove.void addPacketSendingListener(PacketListener packetListener, PacketFilter packetFilter)
packetListener
- the packet listener to notify of sent packets.packetFilter
- the packet filter to use.void removePacketSendingListener(PacketListener packetListener)
packetListener
- the packet listener to remove.void addPacketInterceptor(PacketInterceptor packetInterceptor, PacketFilter packetFilter)
NOTE: For a similar functionality on incoming packets, see addPacketListener(PacketListener, PacketFilter)
.
packetInterceptor
- the packet interceptor to notify of packets about to be sent.packetFilter
- the packet filter to use.void removePacketInterceptor(PacketInterceptor packetInterceptor)
packetInterceptor
- the packet interceptor to remove.java.lang.String getServiceCapsNode()
boolean isRosterVersioningSupported()
long getPacketReplyTimeout()
void setPacketReplyTimeout(long timeout)
SmackException.NoResponseException
if no reply to a request was received within the timeout period.timeout
- the packet reply timeout in millisecondsint getConnectionCounter()
void setFromMode(XMPPConnection.FromMode fromMode)
fromMode
- XMPPConnection.FromMode getFromMode()
XMPPConnection.FromMode
RosterStore getRosterStore()
boolean isRosterLoadedAtLogin()