Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close all sessions when the server closes #37

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/src/realm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class Realm {
_dealer.removeSession(base.id());
}

void stop() {
// stop will disconnect all clients.
Future<void> stop() async {
// concurrently close all sessions
await Future.wait(_clients.values.map((session) => session.close()));
_clients.clear();
}

Future<void> receiveMessage(int sessionID, Message msg) async {
Expand Down
8 changes: 8 additions & 0 deletions lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ class Router {

await _realms[realm]?.receiveMessage(baseSession.id(), msg);
}

Future<void> stop() async {
var realmKeys = _realms.keys.toList();
for (final key in realmKeys) {
var realm = _realms.remove(key);
await realm?.stop();
}
}
}
1 change: 1 addition & 0 deletions lib/src/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Server {
}

Future<void> close() async {
await _router.stop();
await _httpServer.close(force: true);
}

Expand Down