|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.jivesoftware.smack.Connection
public abstract class Connection
The abstract Connection class provides an interface for connections to a XMPP server and implements shared methods which are used by the different types of connections (e.g. XMPPConnection or BoshConnection). 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.
Connection con = new XMPPConnection("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 = connection.getChatManager().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 accross connections.If a connected Connection gets disconnected abruptly then it will try to reconnect again. To stop the reconnection process, use
disconnect(). Once stopped
you can use connect() to manually connect to the server.
XMPPConnection| Field Summary | |
|---|---|
static boolean |
DEBUG_ENABLED
Value that indicates whether debugging is enabled. |
| Method Summary | |
|---|---|
static void |
addConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
Adds a new listener that will be notified when new Connections are created. |
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. |
abstract void |
connect()
Establishes a connection to the XMPP server and performs an automatic login only if the previous connection state was logged (authenticated). |
PacketCollector |
createPacketCollector(PacketFilter packetFilter)
Creates a new packet collector for this connection. |
void |
disconnect()
Closes the connection by setting presence to unavailable then closing the connection to the XMPP server. |
abstract void |
disconnect(Presence unavailablePresence)
Closes the connection. |
AccountManager |
getAccountManager()
Returns an account manager instance for this connection. |
ChatManager |
getChatManager()
Returns a chat manager instance for this connection. |
abstract java.lang.String |
getConnectionID()
Returns the connection ID for this connection, which is the value set by the server when opening a XMPP stream. |
java.lang.String |
getHost()
Returns the host name of the server where the XMPP server is running. |
int |
getPort()
Returns the port number of the XMPP server for this connection. |
abstract Roster |
getRoster()
Returns the roster for the user. |
SASLAuthentication |
getSASLAuthentication()
Returns the SASLAuthentication manager that is responsible for authenticating with the server. |
java.lang.String |
getServiceName()
Returns the name of the service provided by the XMPP server for this connection. |
abstract 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. |
abstract boolean |
isAnonymous()
Returns true if currently authenticated anonymously. |
abstract boolean |
isAuthenticated()
Returns true if currently authenticated by successfully calling the login method. |
abstract boolean |
isConnected()
Returns true if currently connected to the XMPP server. |
abstract boolean |
isSecureConnection()
Returns true if the connection to the server has successfully negotiated encryption. |
abstract boolean |
isUsingCompression()
Returns true if network traffic is being compressed. |
void |
login(java.lang.String username,
java.lang.String password)
Logs in to the server using the strongest authentication mode supported by the server, then sets presence to available. |
abstract void |
login(java.lang.String username,
java.lang.String password,
java.lang.String resource)
Logs in to the server using the strongest authentication mode supported by the server, then sets presence to available. |
abstract void |
loginAnonymously()
Logs in to the server anonymously. |
static void |
removeConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
Removes a listener that was interested in connection creation events. |
void |
removeConnectionListener(ConnectionListener connectionListener)
Removes a connection listener from 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. |
abstract void |
sendPacket(Packet packet)
Sends the specified packet to the server. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static boolean DEBUG_ENABLED
| Method Detail |
|---|
public java.lang.String getServiceName()
public java.lang.String getHost()
public int getPort()
public abstract java.lang.String getUser()
public abstract java.lang.String getConnectionID()
public abstract boolean isConnected()
public abstract boolean isAuthenticated()
public abstract boolean isAnonymous()
public abstract boolean isSecureConnection()
public abstract boolean isUsingCompression()
public abstract void connect()
throws XMPPException
Listeners will be preserved from a previous connection if the reconnection occurs after an abrupt termination.
XMPPException - if an error occurs while trying to establish the connection.
public void login(java.lang.String username,
java.lang.String password)
throws XMPPException
Before logging in (i.e. authenticate) to the server the connection must be connected.
It is possible to log in without sending an initial available presence by using
ConnectionConfiguration.setSendPresence(boolean). If this connection is
not interested in loading its roster upon login then use
ConnectionConfiguration.setRosterLoadedAtLogin(boolean).
Finally, if you want to not pass a password and instead use a more advanced mechanism
while using SASL then you may be interested in using
ConnectionConfiguration.setCallbackHandler(javax.security.auth.callback.CallbackHandler).
For more advanced login settings see ConnectionConfiguration.
username - the username.password - the password or null if using a CallbackHandler.
XMPPException - if an error occurs.
public abstract void login(java.lang.String username,
java.lang.String password,
java.lang.String resource)
throws XMPPException
Before logging in (i.e. authenticate) to the server the connection must be connected.
It is possible to log in without sending an initial available presence by using
ConnectionConfiguration.setSendPresence(boolean). If this connection is
not interested in loading its roster upon login then use
ConnectionConfiguration.setRosterLoadedAtLogin(boolean).
Finally, if you want to not pass a password and instead use a more advanced mechanism
while using SASL then you may be interested in using
ConnectionConfiguration.setCallbackHandler(javax.security.auth.callback.CallbackHandler).
For more advanced login settings see ConnectionConfiguration.
username - the username.password - the password or null if using a CallbackHandler.resource - the resource.
XMPPException - if an error occurs.
java.lang.IllegalStateException - if not connected to the server, or already logged in
to the serrver.
public abstract void loginAnonymously()
throws XMPPException
XMPPException - if an error occurs or anonymous logins are not supported by the server.
java.lang.IllegalStateException - if not connected to the server, or already logged in
to the serrver.public abstract void sendPacket(Packet packet)
packet - the packet to send.public AccountManager getAccountManager()
public ChatManager getChatManager()
public abstract 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
RosterListeners will throw an IllegalStateException.
public SASLAuthentication getSASLAuthentication()
public void disconnect()
This method cleans up all resources used by the connection. Therefore, the roster, listeners and other stateful objects cannot be re-used by simply calling connect() on this connection again. This is unlike the behavior during unexpected disconnects (and subsequent connections). In that case, all state is preserved to allow for more seamless error recovery.
public abstract void disconnect(Presence unavailablePresence)
This method cleans up all resources used by the connection. Therefore, the roster, listeners and other stateful objects cannot be re-used by simply calling connect() on this connection again. This is unlike the behavior during unexpected disconnects (and subsequent connections). In that case, all state is preserved to allow for more seamless error recovery.
unavailablePresence - the presence packet to send during shutdown.public static void addConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
connectionCreationListener - a listener interested on new connections.public static void removeConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
connectionCreationListener - a listener interested on new connections.public void addConnectionListener(ConnectionListener connectionListener)
connectionListener - a connection listener.public void removeConnectionListener(ConnectionListener connectionListener)
connectionListener - a connection listener.public PacketCollector createPacketCollector(PacketFilter packetFilter)
PacketListener when you need to wait for
a specific result.
packetFilter - the packet filter to use.
public void addPacketListener(PacketListener packetListener,
PacketFilter packetFilter)
packetListener - the packet listener to notify of new received packets.packetFilter - the packet filter to use.public void removePacketListener(PacketListener packetListener)
packetListener - the packet listener to remove.
public void addPacketSendingListener(PacketListener packetListener,
PacketFilter packetFilter)
packetListener - the packet listener to notify of sent packets.packetFilter - the packet filter to use.public void removePacketSendingListener(PacketListener packetListener)
packetListener - the packet listener to remove.
public void addPacketInterceptor(PacketInterceptor packetInterceptor,
PacketFilter packetFilter)
packetInterceptor - the packet interceptor to notify of packets about to be sent.packetFilter - the packet filter to use.public void removePacketInterceptor(PacketInterceptor packetInterceptor)
packetInterceptor - the packet interceptor to remove.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||