connect static method

Future<void> connect({
  1. required int subscriptionIndex,
  2. required int serverIndex,
})

Connect to server (legacy method)

Implementation

static Future<void> connect({required int subscriptionIndex, required int serverIndex}) async {
  try {
    if (subscriptionIndex >= _loadedSubscriptions.length) {
      throw Exception('Invalid subscription index: $subscriptionIndex');
    }

    final subscription = _loadedSubscriptions[subscriptionIndex];
    if (serverIndex >= subscription.servers.length) {
      throw Exception('Invalid server index: $serverIndex');
    }

    final server = subscription.servers[serverIndex];
    final config = server.config;

    // Use the first available engine (libxray)
    final engine = VpnEngineFactory.create(EngineType.libxray);
    if (engine == null || !engine.isSupported) {
      throw Exception('LibXray engine is not supported on this platform');
    }

    final success = await engine.connect(config);
    if (success) {
      _updateConnectionStatus(ConnectionStatus.connected);
    } else {
      _updateConnectionStatus(ConnectionStatus.error);
      throw Exception('Failed to connect to server');
    }
  } catch (e) {
    _updateConnectionStatus(ConnectionStatus.error);
    print('Error connecting to server: $e');
    rethrow;
  }
}