Пример #1
0
  /**
   * 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);
  }