Skip to content

Commit a0c7744

Browse files
committed
fix dispose triggering onDispose twice
1 parent 91f73bc commit a0c7744

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

lib/src/support/websocket/io.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,24 @@ class LiveKitWebSocketIO implements LiveKitWebSocket {
2222
]) {
2323
_subscription = _ws.listen(
2424
(dynamic data) => options?.onData?.call(data),
25-
onDone: () => dispose(),
25+
onDone: () async {
26+
await _subscription.cancel();
27+
options?.onDispose?.call();
28+
},
2629
);
2730
}
2831

2932
@override
3033
Future<void> dispose() async {
31-
await _subscription.cancel();
32-
await _ws.close();
33-
options?.onDispose?.call();
34+
if (_ws.readyState != io.WebSocket.closed) {
35+
await _ws.close();
36+
}
3437
}
3538

3639
@override
3740
void send(List<int> data) {
38-
// 0 CONNECTING, 1 OPEN, 2 CLOSING, 3 CLOSED
39-
if (_ws.readyState != 1) {
40-
logger.fine(
41-
'[$objectId] Tried to send data (readyState: ${_ws.readyState})');
41+
if (_ws.readyState != io.WebSocket.open) {
42+
logger.fine('[$objectId] Socket not open (state: ${_ws.readyState})');
4243
return;
4344
}
4445

lib/src/support/websocket/web.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ class LiveKitWebSocketWeb implements LiveKitWebSocket {
2727
dynamic _data = _.data is ByteBuffer ? _.data.asUint8List() : _.data;
2828
options?.onData?.call(_data);
2929
});
30-
_closeSubscription = _ws.onClose.listen((_) => dispose());
30+
_closeSubscription = _ws.onClose.listen((_) async {
31+
await _messageSubscription.cancel();
32+
await _closeSubscription.cancel();
33+
options?.onDispose?.call();
34+
});
3135
}
3236

3337
@override
3438
void send(List<int> data) => _ws.send(data);
3539

3640
@override
3741
Future<void> dispose() async {
38-
options?.onDispose?.call();
39-
await _messageSubscription.cancel();
40-
await _closeSubscription.cancel();
41-
_ws.close();
42+
if (_ws.readyState != html.WebSocket.CLOSED) {
43+
_ws.close();
44+
}
4245
}
4346

4447
static Future<LiveKitWebSocketWeb> connect(

0 commit comments

Comments
 (0)