reconnectClient method

Future<String?> reconnectClient(
  1. SSHModel ssh,
  2. BuildContext context
)

reconnects with the client again every 30 seconds while the app is running with given ssh info

Implementation

Future<String?> reconnectClient(SSHModel ssh, BuildContext context) async {
  String result = '';

  try {
    final socket = await SSHSocket.connect(ssh.host, ssh.port,
        timeout: const Duration(seconds: 36000000));
    String? password;
    bool isAuthenticated = false;

    _client = SSHClient(
      socket,
      onPasswordRequest: () {
        password = ssh.passwordOrKey;

        return password;
      },
      username: ssh.username,
      onAuthenticated: () {
        isAuthenticated = true;
      },
      keepAliveInterval: const Duration(seconds: 36000000),
    );

    /// Add a delay before checking isAuthenticated
    await Future.delayed(const Duration(seconds: 10));

    if (isAuthenticated) {
    } else {
      // If the client is not authenticated, indicate a failed connection
      throw Exception('SSH authentication failed');
    }
  } catch (e) {
    result = "Failed to connect to the SSH server: $e";
  }

  Connectionprovider connection =
      Provider.of<Connectionprovider>(context, listen: false);
  if (result == '') {
    connection.isLgConnected = true;
  } else {
    connection.isLgConnected = false;
  }

  return result;
}