diff --git a/lib/src/realm.dart b/lib/src/realm.dart index 1fbf6f1..0403c83 100644 --- a/lib/src/realm.dart +++ b/lib/src/realm.dart @@ -25,8 +25,10 @@ class Realm { _dealer.removeSession(base.id()); } - void stop() { - // stop will disconnect all clients. + Future stop() async { + // concurrently close all sessions + await Future.wait(_clients.values.map((session) => session.close())); + _clients.clear(); } Future receiveMessage(int sessionID, Message msg) async { diff --git a/lib/src/router.dart b/lib/src/router.dart index 518cc43..74ec448 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -44,4 +44,12 @@ class Router { await _realms[realm]?.receiveMessage(baseSession.id(), msg); } + + Future stop() async { + var realmKeys = _realms.keys.toList(); + for (final key in realmKeys) { + var realm = _realms.remove(key); + await realm?.stop(); + } + } } diff --git a/lib/src/server.dart b/lib/src/server.dart index 0c6b7e7..25fa61d 100644 --- a/lib/src/server.dart +++ b/lib/src/server.dart @@ -53,6 +53,7 @@ class Server { } Future close() async { + await _router.stop(); await _httpServer.close(force: true); }