start method

Future<int> start({
  1. int port = 9090,
  2. String host = '0.0.0.0',
  3. int maxRetries = 0,
  4. bool openDashboard = false,
  5. bool autoAdbForward = true,
})

Implementation

Future<int> start({
  int port = 9090,
  String host = '0.0.0.0',
  int maxRetries = 0,
  bool openDashboard = false,
  bool autoAdbForward = true,
}) async {
  if (_server != null) return _port;

  _port = port;
  NeuronDebugRegistry.instance.enable();

  final router = NeuronDebugRouter(NeuronDebugRegistry.instance);
  final handler = const Pipeline()
      .addMiddleware(logRequests())
      .addMiddleware(_cors())
      .addHandler(router.handler);

  final address = InternetAddress(host);
  for (int attempt = 0; attempt <= maxRetries; attempt++) {
    final candidatePort = port + attempt;
    try {
      _server = await HttpServer.bind(address, candidatePort);
      _port = candidatePort;
      break;
    } on SocketException {
      if (attempt == maxRetries) rethrow;
    }
  }

  _subscription = NeuronDebugStream.instance.stream.listen(_broadcastEvent);
  _heartbeat =
      Timer.periodic(const Duration(seconds: 15), (_) => _sendHeartbeat());

  _server!.listen((HttpRequest request) {
    if (WebSocketTransformer.isUpgradeRequest(request)) {
      _handleWebSocket(request);
      return;
    }
    shelf_io.handleRequest(request, handler);
  });

  // Auto-setup ADB port forwarding for Android development
  if (autoAdbForward) {
    await _setupAdbForward(_port);
  }

  if (openDashboard) {
    _launchDashboard(host, _port);
  }

  return _port;
}