start method

Future<void> start(
  1. String host,
  2. int port, {
  3. int backlog = 0,
  4. bool v6Only = false,
  5. bool shared = false,
  6. void onStartServer()?,
})

Implementation

Future<void> start(
  String host,
  int port, {
  int backlog = 0,
  bool v6Only = false,
  bool shared = false,
  void Function()? onStartServer,
}) async {
  _server = await HttpServer.bind(
    host,
    port,
    backlog: backlog,
    v6Only: v6Only,
    shared: shared,
  );
  onStartServer?.call();

  await for (var req in _server!) {
    // WebSocket upgrade check
    if (WebSocketTransformer.isUpgradeRequest(req)) {
      final socket = await WebSocketTransformer.upgrade(req);
      _socketRouter?.handle(req, socket);
      continue;
    }
    if (_router == null) {
      req.sendHtml('<h1>Router Not Found!</h1>');
    }

    // HTTP request
    final params = <String, String>{};
    final route = _router?.find(
      Method.fromName(req.method),
      req.uri.path,
      params,
    );

    if (route != null) {
      serverParams[req.uri.path] = params;
      route.handler(req);
    } else {
      req.sendNotFoundHtml();
    }
  }
}