public final class XmppClient extends XmppSession
XmppClient xmppClient = XmppClient.create("domain");
xmppClient.connect();
xmppClient.login("username", "password");
By default, the session will try to establish a TCP connection over port 5222 and will try BOSH as fallback.
You can configure a session and its connection methods by passing appropriate configurations in its constructor.
xmppClient.sendMessage(new Message(Jid.of("juliet@example.net"), Message.Type.CHAT));
xmppClient.close();
// Listen for messages
xmppClient.addInboundMessageListener(e ->
// Handle inbound message.
);
// Listen for presence changes
xmppClient.addInboundPresenceListener(e ->
// Handle inbound presence.
);
This class is thread-safe, which means you can safely add listeners or call send(), close() (and other methods) from different threads.XmppSession.Status| Constructor and Description |
|---|
XmppClient(String xmppServiceDomain,
ConnectionConfiguration... connectionConfigurations)
Deprecated.
|
XmppClient(String xmppServiceDomain,
XmppSessionConfiguration configuration,
ConnectionConfiguration... connectionConfigurations)
|
| Modifier and Type | Method and Description |
|---|---|
void |
connect(Jid from)
Connects to the XMPP server.
|
static XmppClient |
create(String xmppServiceDomain,
ConnectionConfiguration... connectionConfigurations)
Creates a new XMPP client instance.
|
static XmppClient |
create(String xmppServiceDomain,
XmppSessionConfiguration configuration,
ConnectionConfiguration... connectionConfigurations)
Creates a new XMPP client instance.
|
Jid |
getConnectedResource()
Gets the connected resource, which is assigned by the server after resource binding.
|
boolean |
isAnonymous()
Indicates whether the session has been logged in anonymously.
|
AsyncResult<Boolean> |
isSupported(String feature,
Jid jid)
Determines support of another XMPP entity for a given feature.
|
byte[] |
login(String authorizationId,
CallbackHandler callbackHandler,
String resource)
Authenticates against the server with a custom callback handler and binds a resource.
|
byte[] |
login(String user,
String password)
Authenticates against the server and binds a random resource (assigned by the server).
|
byte[] |
login(String user,
String password,
String resource)
Authenticates against the server with username/password credential and binds a resource.
|
byte[] |
login(String authorizationId,
String user,
String password,
String resource)
Authenticates against the server with an authorization id and username/password credential and binds a resource.
|
byte[] |
loginAnonymously()
Logs in anonymously and binds a resource.
|
Future<?> |
send(StreamElement element)
Sends an XML element to the server, usually a stanza, i.e. a message, presence or IQ.
|
SendTask<IQ> |
sendIQ(IQ iq)
Sends an IQ.
|
SendTask<Message> |
sendMessage(Message message)
Sends a message.
|
SendTask<Presence> |
sendPresence(Presence presence)
Sends a presence.
|
addConnectionListener, addCreationListener, addInboundIQListener, addInboundMessageListener, addInboundPresenceListener, addIQHandler, addIQHandler, addMessageAcknowledgedListener, addOutboundIQListener, addOutboundMessageListener, addOutboundPresenceListener, addSessionStatusListener, close, connect, createMarshaller, createUnmarshaller, disableFeature, disableFeature, enableFeature, enableFeature, getActiveConnection, getConfiguration, getConnections, getDebugger, getDomain, getEnabledFeatures, getManager, getStatus, getUnacknowledgedStanzas, handleElement, isAuthenticated, isConnected, markAcknowledged, notifyException, query, query, query, removeConnectionListener, removeCreationListener, removeInboundIQListener, removeInboundMessageListener, removeInboundPresenceListener, removeIQHandler, removeMessageAcknowledgedListener, removeOutboundIQListener, removeOutboundMessageListener, removeOutboundPresenceListener, removeSessionStatusListener, sendAndAwaitMessage, sendAndAwaitPresence@Deprecated public XmppClient(String xmppServiceDomain, ConnectionConfiguration... connectionConfigurations)
create(String, ConnectionConfiguration...)xmppServiceDomain - The service domain.connectionConfigurations - The connection configurations.@Deprecated public XmppClient(String xmppServiceDomain, XmppSessionConfiguration configuration, ConnectionConfiguration... connectionConfigurations)
xmppServiceDomain - The service domain.configuration - The configuration.connectionConfigurations - The connection configurations.public static XmppClient create(String xmppServiceDomain, ConnectionConfiguration... connectionConfigurations)
creation listeners are triggered.xmppServiceDomain - The XMPP service domain.connectionConfigurations - The connection methods, which are used to connect.public static XmppClient create(String xmppServiceDomain, XmppSessionConfiguration configuration, ConnectionConfiguration... connectionConfigurations)
creation listeners are triggered.xmppServiceDomain - The XMPP service domain.configuration - The configuration.connectionConfigurations - The connection methods, which are used to connect.public final void connect(Jid from) throws XmppException
connect in class XmppSessionfrom - The 'from' attribute.ConnectionException - If a connection error occurred on the transport layer, e.g. the socket could not connect.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.XmppException - If any other XMPP exception occurs.IllegalStateException - If the session is in a wrong state, e.g. closed or already connected.public final byte[] login(String user, String password) throws XmppException
user - The user name. Usually this is the local part of the user's JID. Must not be null.password - The password. Must not be null.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final byte[] login(String user, String password, String resource) throws XmppException
user - The user name. Usually this is the local part of the user's JID. Must not be null.password - The password. Must not be null.resource - The resource. If null or empty, the resource is randomly assigned by the server.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final byte[] login(String authorizationId, String user, String password, String resource) throws XmppException
authorizationId - The authorization id.user - The user name. Usually this is the local part of the user's JID. Must not be null.password - The password. Must not be null.resource - The resource. If null or empty, the resource is randomly assigned by the server.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final byte[] login(String authorizationId, CallbackHandler callbackHandler, String resource) throws XmppException
authorizationId - The authorization id.callbackHandler - The callback handler.resource - The resource. If null or empty, the resource is randomly assigned by the server.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final byte[] loginAnonymously()
throws XmppException
AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding.XmppException - If the login failed, due to another error.public final Jid getConnectedResource()
After a client has bound a resource to the stream, it is referred to as a "connected resource".
getConnectedResource in class XmppSessionpublic final boolean isAnonymous()
public final Future<?> send(StreamElement element)
XmppSessionsend in class XmppSessionelement - The XML element.Message is translated to a ClientMessage.public final SendTask<IQ> sendIQ(IQ iq)
XmppSessionsendIQ in class XmppSessioniq - The IQ.public final SendTask<Message> sendMessage(Message message)
XmppSessionsendMessage in class XmppSessionmessage - The message.public final SendTask<Presence> sendPresence(Presence presence)
XmppSessionsendPresence in class XmppSessionpresence - The presence.public final AsyncResult<Boolean> isSupported(String feature, Jid jid)
Note that if you want to determine support of another client, you have to provide that client's full JID (user@domain/resource). If you want to determine the server's capabilities provide only the domain JID of the server.
This method uses cached information and the presence based entity capabilities (XEP-0115) to determine support. Only if no information is available an explicit service discovery request is made.feature - The feature, usually defined by an XMPP Extension Protocol, e.g. "urn:xmpp:ping".jid - The XMPP entity.Copyright © 2014–2016 XMPP.rocks. All rights reserved.