private void initPrivateConnection() { long currentUserId = CurrentUser.getInstance().getCurrentUserId(); String currentUserPassword = CurrentUser.getInstance().getCurrentPassword(); String userName = currentUserId + "-" + ApiConstants.APP_ID; XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder(); configBuilder.setUsernameAndPassword(userName, currentUserPassword); configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); configBuilder.setServiceName(ApiConstants.CHAT_END_POINT); configBuilder.setHost(ApiConstants.CHAT_END_POINT); configBuilder.setDebuggerEnabled(true); privateChatConnection = new XMPPTCPConnection(configBuilder.build()); privateChatConnection.addConnectionListener(this); ReconnectionManager manager = ReconnectionManager.getInstanceFor(privateChatConnection); manager.enableAutomaticReconnection(); manager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.FIXED_DELAY); manager.setFixedDelay(15); ProviderManager.addExtensionProvider(Displayed.ELEMENT, Displayed.NAMESPACE, new Displayed.Provider()); DisplayedManager.getInstanceFor(privateChatConnection).addDisplayedListener( (fromJid, toJid, receiptId, receipt) -> { messageService.get().processDisplayed(fromJid, toJid, receiptId, receipt); }); ProviderManager.addExtensionProvider(Received.ELEMENT, Received.NAMESPACE, new Received.Provider()); ReceivedManager.getInstanceFor(privateChatConnection).addReceivedListener( (fromJid, toJid, receiptId, receipt) -> { messageService.get().processReceived(fromJid, toJid, receiptId, receipt); }); }
/** * Configure XMPP connection to use provided ping timeout and reply timeout. */ private void setServerSettings() { // Enable auto-connect ReconnectionManager.getInstanceFor(connection).enableAutomaticReconnection(); // Set reconnection policy to increasing delay ReconnectionManager.getInstanceFor(connection) .setReconnectionPolicy(ReconnectionPolicy.RANDOM_INCREASING_DELAY); // Set ping interval PingManager.getInstanceFor(connection).setPingInterval(pingTimeout); // Specifies when incoming message delivery receipt requests // should be automatically acknowledged with a receipt. DeliveryReceiptManager.getInstanceFor(connection).setAutoReceiptMode(AutoReceiptMode.always); SmackConfiguration.setDefaultPacketReplyTimeout(packetReplyTimeout); logger.debug("Successfully set server settings: {} - {}", new Object[] { pingTimeout, packetReplyTimeout }); }