/** * Check the server connection, reconnect if needed. * * <p>This function will try to ping the server if we are connected, and try to reestablish a * connection otherwise. */ public void sendServerPing() { if (mXMPPConnection == null || !mXMPPConnection.isAuthenticated()) { debugLog("Ping: requested, but not connected to server."); requestConnectionState(ConnectionState.ONLINE, false); return; } if (mPingID != null) { debugLog("Ping: requested, but still waiting for " + mPingID); return; // a ping is still on its way } if (mStreamHandler.isSmEnabled()) { debugLog("Ping: sending SM request"); mPingID = "" + mStreamHandler.requestAck(); } else { Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(mConfig.server); mPingID = ping.getPacketID(); debugLog("Ping: sending ping " + mPingID); mXMPPConnection.sendPacket(ping); } // register ping timeout handler: PACKET_TIMEOUT(30s) + 3s registerPongTimeout(PACKET_TIMEOUT + 3000, mPingID); }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(PING_ALARM)) { if (service.getConnection().isConnected() && service.getConnection().isAuthenticated()) { Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(Common.Service_IP); service.getConnection().sendPacket(ping); ((AlarmManager) this.service.getSystemService(Context.ALARM_SERVICE)) .set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 6000, this.service.getpTimeoutIntent()); // 此时需要启动超时判断的闹钟了,时间间隔为30+3秒 } } else if (intent.getAction().equals(PING_TIMEOUT)) { // ping超时的处理 } }
public void sendServerPing() { if (extXMPPConnection == null || !extXMPPConnection.isAuthenticated()) { requestConnectionState(ConnectionState.ONLINE, false); return; } if (pingID != null) { return; } if (streamHandler.isIfEnabled()) { pingID = "" + streamHandler.requestAck(); } else { Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(configuration.server); pingID = ping.getPacketID(); extXMPPConnection.sendPacket(ping); } registerPongTimeout(PACKET_TIMEOUT + 3000, pingID); }