管理连接

创建一个连接

org.jivesoftware.smack.XMPPConnection管理你的XMPP服务器的连接,默认实现类为org.jivesoftware.smack.XMPPTCPConnection。 主要是使用两个构造函数,第一个是 XMPPTCPConnection(String) ,它把 你想连接到服务器名称 作为参数。

连接和断开

// Create the configuration for this new connection
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("username", "password");
configBuilder.setResource("SomeResource");
configBuilder.setServiceName("jabber.org");

AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
connection.connect();
// Login into the server
connection.login();

...

// Disconnect from the server
connection.disconnect();

默认情况下打将尝试重新连接,以防突然断开连接。使用 ConnectionConfiguration#setReconnectionAllowed(boolean) 来打开/关闭此功能。立即重新连接管理器将在一直失败的时候,会连续增加重连的延迟时长来尝试重新连接。

如果你想强迫重新连接而 reconnetion manager 正在等待下一个重新连接,您可以使用AbstractXMPPConnection # connect() 创建一个新的尝试。如果手动尝试也失败了,那么重新连接管理器会继续重新连接工作。