public class XMPPConnection
extends java.lang.Object
// Create a connection to the jivesoftware.com XMPP server. XMPPConnection con = new XMPPConnection("jivesoftware.com"); // 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 = con.createChat("jdoe@jabber.org"); chat.sendMessage("Hey, how's it going?");
Modifier and Type | Field and Description |
---|---|
static boolean |
DEBUG_ENABLED
Value that indicates whether debugging is enabled.
|
Constructor and Description |
---|
XMPPConnection(ConnectionConfiguration config) |
XMPPConnection(ConnectionConfiguration config,
javax.net.SocketFactory socketFactory) |
XMPPConnection(java.lang.String serviceName)
Creates a new connection to the specified XMPP server.
|
XMPPConnection(java.lang.String host,
int port)
Creates a new connection to the XMPP server at the specifiec host and port.
|
XMPPConnection(java.lang.String host,
int port,
java.lang.String serviceName)
Creates a new connection to the specified XMPP server on the given host and port.
|
XMPPConnection(java.lang.String host,
int port,
java.lang.String serviceName,
javax.net.SocketFactory socketFactory)
Creates a new connection to the specified XMPP server on the given port using the
specified SocketFactory.
|
Modifier and Type | Method and Description |
---|---|
static void |
addConnectionListener(ConnectionEstablishedListener connectionEstablishedListener)
Adds a connection established listener that will be notified when a new connection
is established.
|
void |
addConnectionListener(ConnectionListener connectionListener)
Adds a connection listener to this connection that will be notified when
the connection closes or fails.
|
void |
addPacketListener(PacketListener packetListener,
PacketFilter packetFilter)
Registers a packet listener with this connection.
|
void |
addPacketWriterInterceptor(PacketInterceptor packetInterceptor,
PacketFilter packetFilter)
Registers a packet interceptor with this connection.
|
void |
addPacketWriterListener(PacketListener packetListener,
PacketFilter packetFilter)
Registers a packet listener with this connection.
|
void |
close()
Closes the connection by setting presence to unavailable then closing the stream to
the XMPP server.
|
Chat |
createChat(java.lang.String participant)
Creates a new chat with the specified participant.
|
GroupChat |
createGroupChat(java.lang.String room)
Creates a new group chat connected to the specified room.
|
PacketCollector |
createPacketCollector(PacketFilter packetFilter)
Creates a new packet collector for this connection.
|
AccountManager |
getAccountManager()
Returns an account manager instance for this connection.
|
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.
|
Roster |
getRoster()
Returns the roster for the user logged into the server.
|
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.
|
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 |
isSecureConnection()
Returns true if the connection is a secured one, such as an SSL connection or
if TLS was negotiated successfully.
|
boolean |
isUsingCompression()
Returns true if network traffic is being compressed.
|
boolean |
isUsingTLS()
Returns true if the connection to the server has successfully negotiated TLS.
|
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 set our presence to available.
|
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.
|
void |
login(java.lang.String username,
java.lang.String password,
java.lang.String resource,
boolean sendPresence)
Logs in to the server using the strongest authentication mode supported by
the server.
|
void |
loginAnonymously()
Logs in to the server anonymously.
|
static void |
removeConnectionListener(ConnectionEstablishedListener connectionEstablishedListener)
Removes a listener on new established connections.
|
void |
removeConnectionListener(ConnectionListener connectionListener)
Removes a connection listener from this connection.
|
void |
removePacketListener(PacketListener packetListener)
Removes a packet listener from this connection.
|
void |
removePacketWriterInterceptor(PacketInterceptor packetInterceptor)
Removes a packet interceptor.
|
void |
removePacketWriterListener(PacketListener packetListener)
Removes a packet listener from this connection.
|
void |
sendPacket(Packet packet)
Sends the specified packet to the server.
|
public static boolean DEBUG_ENABLED
public XMPPConnection(java.lang.String serviceName) throws XMPPException
serviceName
- the name of the XMPP server to connect to; e.g. jivesoftware.com.XMPPException
- if an error occurs while trying to establish the connection.
Two possible errors can occur which will be wrapped by an XMPPException --
UnknownHostException (XMPP error code 504), and IOException (XMPP error code
502). The error codes and wrapped exceptions can be used to present more
appropiate error messages to end-users.public XMPPConnection(java.lang.String host, int port) throws XMPPException
host
- the name of the XMPP server to connect to; e.g. jivesoftware.com.port
- the port on the server that should be used; e.g. 5222.XMPPException
- if an error occurs while trying to establish the connection.
Two possible errors can occur which will be wrapped by an XMPPException --
UnknownHostException (XMPP error code 504), and IOException (XMPP error code
502). The error codes and wrapped exceptions can be used to present more
appropiate error messages to end-users.public XMPPConnection(java.lang.String host, int port, java.lang.String serviceName) throws XMPPException
host
- the host name, or null for the loopback address.port
- the port on the server that should be used; e.g. 5222.serviceName
- the name of the XMPP server to connect to; e.g. jivesoftware.com.XMPPException
- if an error occurs while trying to establish the connection.
Two possible errors can occur which will be wrapped by an XMPPException --
UnknownHostException (XMPP error code 504), and IOException (XMPP error code
502). The error codes and wrapped exceptions can be used to present more
appropiate error messages to end-users.public XMPPConnection(java.lang.String host, int port, java.lang.String serviceName, javax.net.SocketFactory socketFactory) throws XMPPException
A custom SocketFactory allows fine-grained control of the actual connection to the XMPP server. A typical use for a custom SocketFactory is when connecting through a SOCKS proxy.
host
- the host name, or null for the loopback address.port
- the port on the server that should be used; e.g. 5222.serviceName
- the name of the XMPP server to connect to; e.g. jivesoftware.com.socketFactory
- a SocketFactory that will be used to create the socket to the XMPP
server.XMPPException
- if an error occurs while trying to establish the connection.
Two possible errors can occur which will be wrapped by an XMPPException --
UnknownHostException (XMPP error code 504), and IOException (XMPP error code
502). The error codes and wrapped exceptions can be used to present more
appropiate error messages to end-users.public XMPPConnection(ConnectionConfiguration config) throws XMPPException
XMPPException
public XMPPConnection(ConnectionConfiguration config, javax.net.SocketFactory socketFactory) throws XMPPException
XMPPException
public java.lang.String getConnectionID()
public java.lang.String getServiceName()
public java.lang.String getHost()
public int getPort()
public java.lang.String getUser()
public void login(java.lang.String username, java.lang.String password) throws XMPPException
username
- the username.password
- the password.XMPPException
- if an error occurs.public void login(java.lang.String username, java.lang.String password, java.lang.String resource) throws XMPPException
username
- the username.password
- the password.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 void login(java.lang.String username, java.lang.String password, java.lang.String resource, boolean sendPresence) throws XMPPException
username
- the username.password
- the password.resource
- the resource.sendPresence
- if true an available presence will be sent automatically
after login is completed.XMPPException
- if an error occurs.java.lang.IllegalStateException
- if not connected to the server, or already logged in
to the serrver.public 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 Roster getRoster()
public AccountManager getAccountManager()
public Chat createChat(java.lang.String participant)
participant
- the person to start the conversation with.public GroupChat createGroupChat(java.lang.String room)
Most XMPP servers use a sub-domain for the chat service (eg chat.example.com for the XMPP server example.com). You must ensure that the room address you're trying to connect to includes the proper chat sub-domain.
room
- the fully qualifed name of the room.public boolean isConnected()
public boolean isSecureConnection()
public boolean isAuthenticated()
public boolean isAnonymous()
public void close()
public void sendPacket(Packet packet)
packet
- the packet to send.public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter)
packetListener
- the packet listener to notify of new packets.packetFilter
- the packet filter to use.public void removePacketListener(PacketListener packetListener)
packetListener
- the packet listener to remove.public void addPacketWriterListener(PacketListener packetListener, PacketFilter packetFilter)
packetListener
- the packet listener to notify of sent packets.packetFilter
- the packet filter to use.public void removePacketWriterListener(PacketListener packetListener)
packetListener
- the packet listener to remove.public void addPacketWriterInterceptor(PacketInterceptor packetInterceptor, PacketFilter packetFilter)
packetInterceptor
- the packet interceptor to notify of packets about to be sent.packetFilter
- the packet filter to use.public void removePacketWriterInterceptor(PacketInterceptor packetInterceptor)
packetInterceptor
- the packet interceptor to remove.public PacketCollector createPacketCollector(PacketFilter packetFilter)
packetFilter
- the packet filter to use.public void addConnectionListener(ConnectionListener connectionListener)
connectionListener
- a connection listener.public void removeConnectionListener(ConnectionListener connectionListener)
connectionListener
- a connection listener.public static void addConnectionListener(ConnectionEstablishedListener connectionEstablishedListener)
connectionEstablishedListener
- a listener interested on connection established events.public static void removeConnectionListener(ConnectionEstablishedListener connectionEstablishedListener)
connectionEstablishedListener
- a listener interested on connection established events.public boolean isUsingTLS()
public SASLAuthentication getSASLAuthentication()
public boolean isUsingCompression()
Note: To use stream compression the smackx.jar file has to be present in the classpath.
Copyright © 2003 Jive Software.