close method

Future<void> close({
  1. bool force = false,
})

Closes the underlying TCP server.

When force is true, all active client sockets are torn down before the method returns, and this call still waits for in-flight session cleanup to finish.

Implementation

Future<void> close({bool force = false}) async {
  if (_closed) return;
  _closed = true;
  await _subscription.cancel();
  await server.close();

  if (force) {
    await Future.wait(
      _activeSockets
          .toList(growable: false)
          .map((socket) => Future.sync(socket.close)),
      eagerError: false,
    );
    for (final socket in _activeSockets.toList(growable: false)) {
      socket.destroy();
    }
  }

  if (_activeSessions.isNotEmpty) {
    await Future.wait(
      _activeSessions.toList(growable: false),
      eagerError: false,
    );
  }
}