connectWs method

Future<String> connectWs(
  1. String wsUrl
)

Test WebSocket connectivity to a URL Returns the URL if successful, throws otherwise

Implementation

Future<String> connectWs(String wsUrl) async {
  try {
    // Try to connect to the WebSocket URL
    // This is a basic test; actual WebSocket connection happens later
    final httpUrl = wsUrl.replaceFirst(RegExp(r'^wss?://'), 'https://');
    final response = await _httpClient
        .head(Uri.parse(httpUrl))
        .timeout(const Duration(seconds: 5));

    if (response.statusCode >= 400) {
      throw ServerException(
        'WebSocket URL returned error: ${response.statusCode}',
      );
    }

    return wsUrl;
  } catch (e) {
    if (!_isRecoverableError(e)) {
      rethrow;
    }
    _tryNextServer();
    return connectWs(workingUrl.replaceFirst('http', 'ws'));
  }
}