1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +01:00
Smack/documentation/connections.md

59 lines
3 KiB
Markdown
Raw Normal View History

2014-08-16 00:09:55 +02:00
Smack: XMPPConnection Management
================================
[Back](index.md)
2014-08-16 00:09:55 +02:00
Creating a Connection
---------------------
2018-04-01 15:43:29 +02:00
The `org.jivesoftware.smack.XMPPConnection` interface manages your connection to
2014-08-16 00:09:55 +02:00
an XMPP server. The default implementation is the
2018-04-01 15:43:29 +02:00
`org.jivesoftware.smack.tcp.XMPPTCPConnection` class. The class contains three constructors. The simplest, `XMPPTCPConnection(CharSequence, String, String)` takes the username, password, and server name you'd like
to connect to as arguments. All default connection settings will be used:
2014-08-16 00:09:55 +02:00
* A DNS SRV lookup will be performed to find the exact address and port (typically 5222) that the server resides at.
* The maximum security possible will be negotiated with the server, including TLS encryption, but the connection will fall back to lower security settings if necessary.
* The XMPP resource name "Smack" will be used for the connection.
Alternatively, you can use the `XMPPTCPConnection(ConnectionConfiguration)`
constructor to specify advanced connection settings. Some of these settings
include:
* Manually specify the server address and port of the server rather than using a DNS SRV lookup.
* Enable connection compression.
* Customize security settings, such as flagging the connection to require TLS encryption in order to connect.
* Specify a custom connection resource name such as "Work" or "Home". Every connection by a user to a server must have a unique resource name. For the user "jsmith@example.com", the full address with resource might be "jsmith@example.com/Smack". With unique resource names, a user can be logged into the server from multiple locations at once, or using multiple devices. The presence priority value used with each resource will determine which particular connection receives messages to the bare address ("jsmith@example.com" in our example).
Connect and Disconnect
----------------------
```
// Create the configuration for this new connection
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("username", "password");
configBuilder.setResource("SomeResource");
configBuilder.setXmppDomain("jabber.org");
AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
2014-08-16 00:09:55 +02:00
connection.connect();
// Log into the server
connection.login();
2014-08-16 00:09:55 +02:00
...
// Disconnect from the server
2014-08-16 00:09:55 +02:00
connection.disconnect();
```
By default Smack will try to reconnect the connection in case it was abruptly
2018-04-01 15:43:29 +02:00
disconnected. The reconnection manager will try to immediately
2014-08-16 00:09:55 +02:00
reconnect to the server and increase the delay between attempts as successive
reconnections keep failing._
2018-04-01 15:43:29 +02:00
In case you want to force a reconnection while the reconnection manager is
2014-08-16 00:09:55 +02:00
waiting for the next reconnection, you can just use _AbstractXMPPConnection#connect()_
and a new attempt will be made. If the manual attempt also failed then the
reconnection manager will still continue the reconnection job.
Copyright (C) Jive Software 2002-2008