Skip to content

Commit

Permalink
Merge pull request #18 from xconnio/detach-on-client-disconnect
Browse files Browse the repository at this point in the history
Detach client on abrupt client disconnect
  • Loading branch information
Mahad-10 authored May 11, 2024
2 parents 9ee9de3 + 9a38f53 commit b3ba036
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 1 addition & 3 deletions lib/src/acceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ class WAMPSessionAcceptor {

IServerAuthenticator? _authenticator;
late Serializer _serializer;
late StreamSubscription wsStreamSubscription;

Future<BaseSession> accept(WebSocket ws) async {
_serializer = getSerializer(ws.protocol);
Acceptor acceptor = Acceptor(serializer: _serializer, authenticator: _authenticator);

Completer<BaseSession> completer = Completer<BaseSession>();

// ignore: cancel_subscriptions
late StreamSubscription<dynamic> wsStreamSubscription;

wsStreamSubscription = ws.listen((message) {
MapEntry<Object, bool> received = acceptor.receive(message);
ws.add(received.key);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class Server {
_router.attachClient(baseSession);

_handleWebSocket(baseSession, webSocket);
acceptor.wsStreamSubscription.onDone(() {
_router.detachClient(baseSession);
});
}
}

Expand Down
7 changes: 6 additions & 1 deletion lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ abstract class IBaseSession {
}

class BaseSession extends IBaseSession {
BaseSession(this._ws, this._wsStreamSubscription, this.sessionDetails, this._serializer);
BaseSession(this._ws, this._wsStreamSubscription, this.sessionDetails, this._serializer) {
// close cleanly on abrupt client disconnect
_wsStreamSubscription.onDone(() async {
await close();
});
}

final WebSocket _ws;
final StreamSubscription<dynamic> _wsStreamSubscription;
Expand Down

0 comments on commit b3ba036

Please sign in to comment.