public interface XMPPConnection
XMPPTCPConnection
or XMPPBOSHConnection
). To create a connection to an XMPP server a simple usage of this API might look like the following:
// Create a connection to the igniterealtime.org XMPP server. XMPPTCPConnection 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();
Note that the XMPPConnection interface does intentionally not declare any methods that manipulate the connection state, e.g. connect()
, disconnect()
. You should use the most specific connection type, e.g. XMPPTCPConnection
as declared type and use the XMPPConnection interface when you don't need to manipulate the connection state.
XMPPConnections can be reused between connections. This means that an Connection may be connected, disconnected and then connected again. Listeners of the XMPPConnection will be retained across connections.
Modifier and Type | Interface and Description |
---|---|
static class | XMPPConnection.FromMode |
Modifier and Type | Method and Description |
---|---|
void | addAsyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) Registers an asynchronous stanza(/packet) listener with this connection. |
void | addConnectionListener(ConnectionListener connectionListener) Adds a connection listener to this connection that will be notified when the connection closes or fails. |
void | addOneTimeSyncCallback(StanzaListener callback, StanzaFilter packetFilter) Add a callback that is called exactly once and synchronously with the incoming stanza that matches the given stanza(/packet) filter. |
void | addPacketInterceptor(StanzaListener packetInterceptor, StanzaFilter packetFilter) Registers a stanza(/packet) interceptor with this connection. |
void | addPacketListener(StanzaListener packetListener, StanzaFilter packetFilter) |
void | addPacketSendingListener(StanzaListener packetListener, StanzaFilter packetFilter) Registers a stanza(/packet) listener with this connection. |
void | addSyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) Registers a synchronous stanza(/packet) listener with this connection. |
PacketCollector | createPacketCollector(PacketCollector.Configuration configuration) Create a new stanza(/packet) collector with the given stanza(/packet) collector configuration. |
PacketCollector | createPacketCollector(StanzaFilter packetFilter) Creates a new stanza(/packet) collector for this connection. |
PacketCollector | createPacketCollectorAndSend(IQ packet) Creates a new stanza(/packet) collector collecting packets that are replies to packet . |
PacketCollector | createPacketCollectorAndSend(StanzaFilter packetFilter, Stanza packet) Creates a new stanza(/packet) collector for this connection. |
int | getConnectionCounter() Get the connection counter of this XMPPConnection instance. |
<F extends ExtensionElement> | getFeature(java.lang.String element, java.lang.String namespace) Get the feature stanza(/packet) extensions for a given stream feature of the server, or null if the server doesn't support that feature. |
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 | getLastStanzaReceived() Returns the timestamp in milliseconds when the last stanza was received. |
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. |
java.lang.String | getServiceName() Returns the name of the service provided by the XMPP server for this connection. |
java.lang.String | getStreamId() Returns the stream ID for this connection, which is the value set by the server when opening an XMPP stream. |
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 | hasFeature(java.lang.String element, java.lang.String namespace) Return true if the server supports the given stream feature. |
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 | isSecureConnection() Returns true if the connection to the server has successfully negotiated encryption. |
boolean | isUsingCompression() Returns true if network traffic is being compressed. |
IQRequestHandler | registerIQRequestHandler(IQRequestHandler iqRequestHandler) Register an IQ request handler with this connection. |
boolean | removeAsyncStanzaListener(StanzaListener packetListener) Removes an asynchronous stanza(/packet) listener for received packets from this connection. |
void | removeConnectionListener(ConnectionListener connectionListener) Removes a connection listener from this connection. |
void | removePacketCollector(PacketCollector collector) Remove a stanza(/packet) collector of this connection. |
void | removePacketInterceptor(StanzaListener packetInterceptor) Removes a stanza(/packet) interceptor. |
boolean | removePacketListener(StanzaListener packetListener) Deprecated. |
void | removePacketSendingListener(StanzaListener packetListener) Removes a stanza(/packet) listener for sending packets from this connection. |
boolean | removeSyncStanzaListener(StanzaListener packetListener) Removes a stanza(/packet) listener for received packets from this connection. |
void | send(PlainStreamElement element) Send a PlainStreamElement. |
void | sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback) Send a IQ stanza and invoke callback if there is a result of IQ.Type.result with that result IQ. |
void | sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback, ExceptionCallback exceptionCallback) Send a IQ stanza and invoke callback if there is a result of IQ.Type.result with that result IQ. |
void | sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback, ExceptionCallback exceptionCallback, long timeout) Send a IQ stanza and invoke callback if there is a result of IQ.Type.result with that result IQ. |
void | sendPacket(Stanza packet) Deprecated. use sendStanza(Stanza) instead. |
void | sendStanza(Stanza stanza) Sends the specified stanza to the server. |
void | sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter, StanzaListener callback) Send a stanza and wait asynchronously for a response by using replyFilter . |
void | sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter, StanzaListener callback, ExceptionCallback exceptionCallback) Send a stanza and wait asynchronously for a response by using replyFilter . |
void | sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter, StanzaListener callback, ExceptionCallback exceptionCallback, long timeout) Send a stanza and wait asynchronously for a response by using replyFilter . |
void | setFromMode(XMPPConnection.FromMode fromMode) Set the FromMode for this connection instance. |
void | setPacketReplyTimeout(long timeout) Set the stanza(/packet) reply timeout in milliseconds. |
IQRequestHandler | unregisterIQRequestHandler(IQRequestHandler iqRequestHandler) Convenience method for unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type) . |
IQRequestHandler | unregisterIQRequestHandler(java.lang.String element, java.lang.String namespace, IQ.Type type) Unregister an IQ request handler with this connection. |
java.lang.String getServiceName()
java.lang.String getHost()
int getPort()
java.lang.String getUser()
java.lang.String getStreamId()
boolean isConnected()
boolean isAuthenticated()
boolean isAnonymous()
boolean isSecureConnection()
boolean isUsingCompression()
@Deprecatedvoid sendPacket(Stanza packet) throws SmackException.NotConnectedException
sendStanza(Stanza)
instead.packet
- the stanza(/packet) to send.SmackException.NotConnectedException
void sendStanza(Stanza stanza) throws SmackException.NotConnectedException
stanza
- the stanza to send.SmackException.NotConnectedException
- if the connection is not connected.void send(PlainStreamElement element) throws SmackException.NotConnectedException
This method is not meant for end-user usage! It allows sending plain stream elements, which should not be done by a user manually. Doing so may result in a unstable or unusable connection. Certain Smack APIs use this method to send plain stream elements.
element
- SmackException.NotConnectedException
void 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 stanza(/packet) filter for the collector is an IQReplyFilter
, guaranteeing that stanza(/packet) id and JID in the 'from' address have expected values.packet
- the stanza(/packet) to filter responses fromSmackException.NotConnectedException
PacketCollector createPacketCollectorAndSend(StanzaFilter packetFilter, Stanza packet) throws SmackException.NotConnectedException
StanzaListener
when you need to wait for a specific result.packetFilter
- the stanza(/packet) filter to use.packet
- the stanza(/packet) to send right after the collector got createdSmackException.NotConnectedException
PacketCollector createPacketCollector(StanzaFilter packetFilter)
StanzaListener
when you need to wait for a specific result. Note: If you send a Stanza(/Packet) right after using this method, then consider using createPacketCollectorAndSend(StanzaFilter, Stanza)
instead. Otherwise make sure cancel the PacketCollector in every case, e.g. even if an exception is thrown, or otherwise you may leak the PacketCollector.
packetFilter
- the stanza(/packet) filter to use.PacketCollector createPacketCollector(PacketCollector.Configuration configuration)
Please make sure to cancel the collector when it is no longer required. See also createPacketCollector(StanzaFilter)
.
configuration
- the stanza(/packet) collector configuration.void removePacketCollector(PacketCollector collector)
collector
- a stanza(/packet) collectors which was created for this connection.@Deprecatedvoid addPacketListener(StanzaListener packetListener, StanzaFilter packetFilter)
addAsyncStanzaListener(StanzaListener, StanzaFilter)
or addSyncStanzaListener(StanzaListener, StanzaFilter)
. This method has been deprecated. It is important to differentiate between using an asynchronous stanza(/packet) listener (preferred where possible) and a synchronous stanza(/packet) lister. Refer addAsyncStanzaListener(StanzaListener, StanzaFilter)
and addSyncStanzaListener(StanzaListener, StanzaFilter)
for more information.
packetListener
- the stanza(/packet) listener to notify of new received packets.packetFilter
- the stanza(/packet) filter to use.@Deprecatedboolean removePacketListener(StanzaListener packetListener)
removeAsyncStanzaListener(StanzaListener)
or removeSyncStanzaListener(StanzaListener)
.packetListener
- the stanza(/packet) listener to remove.void addSyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter)
Important: This stanza(/packet) listeners will be called in the same single thread that processes all incoming stanzas. Only use this kind of stanza(/packet) filter if it does not perform any XMPP activity that waits for a response. Consider using addAsyncStanzaListener(StanzaListener, StanzaFilter)
when possible, i.e. when the invocation order doesn't have to be the same as the order of the arriving packets. If the order of the arriving packets, consider using a PacketCollector
when possible.
packetListener
- the stanza(/packet) listener to notify of new received packets.packetFilter
- the stanza(/packet) filter to use.addPacketInterceptor(StanzaListener, StanzaFilter)
boolean removeSyncStanzaListener(StanzaListener packetListener)
packetListener
- the stanza(/packet) listener to remove.void addAsyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter)
Unlike addAsyncStanzaListener(StanzaListener, StanzaFilter)
stanza(/packet) listeners added with this method will be invoked asynchronously in their own thread. Use this method if the order of the stanza(/packet) listeners must not depend on the order how the stanzas where received.
packetListener
- the stanza(/packet) listener to notify of new received packets.packetFilter
- the stanza(/packet) filter to use.addPacketInterceptor(StanzaListener, StanzaFilter)
boolean removeAsyncStanzaListener(StanzaListener packetListener)
packetListener
- the stanza(/packet) listener to remove.void addPacketSendingListener(StanzaListener packetListener, StanzaFilter packetFilter)
packetListener
- the stanza(/packet) listener to notify of sent packets.packetFilter
- the stanza(/packet) filter to use.void removePacketSendingListener(StanzaListener packetListener)
packetListener
- the stanza(/packet) listener to remove.void addPacketInterceptor(StanzaListener packetInterceptor, StanzaFilter packetFilter)
NOTE: For a similar functionality on incoming packets, see addAsyncStanzaListener(StanzaListener, StanzaFilter)
.
packetInterceptor
- the stanza(/packet) interceptor to notify of packets about to be sent.packetFilter
- the stanza(/packet) filter to use.void removePacketInterceptor(StanzaListener packetInterceptor)
packetInterceptor
- the stanza(/packet) interceptor to remove.long getPacketReplyTimeout()
void setPacketReplyTimeout(long timeout)
SmackException.NoResponseException
if no reply to a request was received within the timeout period.timeout
- the stanza(/packet) reply timeout in millisecondsint getConnectionCounter()
void setFromMode(XMPPConnection.FromMode fromMode)
fromMode
- XMPPConnection.FromMode getFromMode()
XMPPConnection.FromMode
<F extends ExtensionElement> F getFeature(java.lang.String element, java.lang.String namespace)
null
if the server doesn't support that feature.element
- namespace
- null
boolean hasFeature(java.lang.String element, java.lang.String namespace)
element
- namespace
- void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter, StanzaListener callback) throws SmackException.NotConnectedException
replyFilter
. If there is a response, then callback
will be invoked. The callback will be invoked at most once and it will be not invoked after the connections default reply timeout has been elapsed.
stanza
- the stanza to send (required)replyFilter
- the filter used to determine response stanza (required)callback
- the callback invoked if there is a response (required)SmackException.NotConnectedException
void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter, StanzaListener callback, ExceptionCallback exceptionCallback) throws SmackException.NotConnectedException
replyFilter
. If there is a response, then callback
will be invoked. If there is no response after the connections default reply timeout, then exceptionCallback
will be invoked with a SmackException.NoResponseException
. The callback will be invoked at most once.
stanza
- the stanza to send (required)replyFilter
- the filter used to determine response stanza (required)callback
- the callback invoked if there is a response (required)exceptionCallback
- the callback invoked if there is an exception (optional)SmackException.NotConnectedException
void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter, StanzaListener callback, ExceptionCallback exceptionCallback, long timeout) throws SmackException.NotConnectedException
replyFilter
. If there is a response, then callback
will be invoked. If there is no response after timeout
milliseconds, then exceptionCallback
will be invoked with a SmackException.NoResponseException
. The callback will be invoked at most once.
stanza
- the stanza to send (required)replyFilter
- the filter used to determine response stanza (required)callback
- the callback invoked if there is a response (required)exceptionCallback
- the callback invoked if there is an exception (optional)timeout
- the timeout in milliseconds to wait for a responseSmackException.NotConnectedException
void sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback) throws SmackException.NotConnectedException
callback
if there is a result of IQ.Type.result
with that result IQ. The callback will not be invoked after the connections default reply timeout has been elapsed.iqRequest
- the IQ stanza to send (required)callback
- the callback invoked if there is result response (required)SmackException.NotConnectedException
void sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback, ExceptionCallback exceptionCallback) throws SmackException.NotConnectedException
callback
if there is a result of IQ.Type.result
with that result IQ. If there is an error response exceptionCallback
will be invoked, if not null, with the received error as XMPPException.XMPPErrorException
. If there is no response after the connections default reply timeout, then exceptionCallback
will be invoked with a SmackException.NoResponseException
.iqRequest
- the IQ stanza to send (required)callback
- the callback invoked if there is result response (required)exceptionCallback
- the callback invoked if there is an Exception optionalSmackException.NotConnectedException
void sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback, ExceptionCallback exceptionCallback, long timeout) throws SmackException.NotConnectedException
callback
if there is a result of IQ.Type.result
with that result IQ. If there is an error response exceptionCallback
will be invoked, if not null, with the received error as XMPPException.XMPPErrorException
. If there is no response after timeout
, then exceptionCallback
will be invoked with a SmackException.NoResponseException
.iqRequest
- the IQ stanza to send (required)callback
- the callback invoked if there is result response (required)exceptionCallback
- the callback invoked if there is an Exception optionaltimeout
- the timeout in milliseconds to wait for a responseSmackException.NotConnectedException
void addOneTimeSyncCallback(StanzaListener callback, StanzaFilter packetFilter)
callback
- the callback invoked once the stanza(/packet) filter matches a stanza.packetFilter
- the filter to match stanzas or null to match all.IQRequestHandler registerIQRequestHandler(IQRequestHandler iqRequestHandler)
IQ request handler process incoming IQ requests, i.e. incoming IQ stanzas of type 'get' or 'set', and return a result.
iqRequestHandler
- the IQ request handler to register.IQRequestHandler unregisterIQRequestHandler(IQRequestHandler iqRequestHandler)
unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type)
.iqRequestHandler
- IQRequestHandler unregisterIQRequestHandler(java.lang.String element, java.lang.String namespace, IQ.Type type)
element
- the IQ element the IQ request handler is responsible for.namespace
- the IQ namespace the IQ request handler is responsible for.type
- the IQ type the IQ request handler is responsible for.long getLastStanzaReceived()