ignore new data on disposed socket

This commit is contained in:
Hiroshi Horie
2021-12-23 03:06:43 +07:00
parent 8f0e73487b
commit be01553ac2
4 changed files with 29 additions and 22 deletions
+1 -4
View File
@@ -259,7 +259,6 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
_reconnectAttempts++; _reconnectAttempts++;
try { try {
// isReconnecting = true;
await signalClient.reconnect( await signalClient.reconnect(
url, url,
token, token,
@@ -272,7 +271,6 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
subscriber!.restartingIce = true; subscriber!.restartingIce = true;
// await negotiate(iceRestart: true);
if (_hasPublished) { if (_hasPublished) {
logger.fine('Reconnect: negotiating publisher...'); logger.fine('Reconnect: negotiating publisher...');
await publisher!.createAndSendOffer(const RTCOfferOptions( await publisher!.createAndSendOffer(const RTCOfferOptions(
@@ -604,8 +602,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
..on<SignalStreamStateUpdatedEvent>((event) => events.emit(event)) ..on<SignalStreamStateUpdatedEvent>((event) => events.emit(event))
..on<SignalLeaveEvent>((event) async { ..on<SignalLeaveEvent>((event) async {
if (connectionState == ConnectionState.reconnecting) { if (connectionState == ConnectionState.reconnecting) {
logger.fine('Ignoring leave signal since engine is reconnecting...'); logger.warning('Received leave signal while engine is reconnecting.');
return;
} }
await close(); await close();
events.emit(const EngineDisconnectedEvent()); events.emit(const EngineDisconnectedEvent());
+2 -2
View File
@@ -1,3 +1,4 @@
import '../support/disposable.dart';
import 'websocket/io.dart' if (dart.library.html) 'websocket/web.dart'; import 'websocket/io.dart' if (dart.library.html) 'websocket/web.dart';
class WebSocketException implements Exception { class WebSocketException implements Exception {
@@ -29,9 +30,8 @@ class WebSocketEventHandlers {
}); });
} }
abstract class LiveKitWebSocket { abstract class LiveKitWebSocket extends Disposable {
void send(List<int> data); void send(List<int> data);
Future<void> dispose();
static Future<LiveKitWebSocket> connect( static Future<LiveKitWebSocket> connect(
Uri uri, [ Uri uri, [
+13 -8
View File
@@ -11,7 +11,7 @@ Future<LiveKitWebSocketIO> lkWebSocketConnect(
]) => ]) =>
LiveKitWebSocketIO.connect(uri, options); LiveKitWebSocketIO.connect(uri, options);
class LiveKitWebSocketIO implements LiveKitWebSocket { class LiveKitWebSocketIO extends LiveKitWebSocket {
final io.WebSocket _ws; final io.WebSocket _ws;
final WebSocketEventHandlers? options; final WebSocketEventHandlers? options;
late final StreamSubscription _subscription; late final StreamSubscription _subscription;
@@ -21,19 +21,24 @@ class LiveKitWebSocketIO implements LiveKitWebSocket {
this.options, this.options,
]) { ]) {
_subscription = _ws.listen( _subscription = _ws.listen(
(dynamic data) => options?.onData?.call(data), (dynamic data) {
if (isDisposed) {
logger.warning('$objectId already disposed, ignoring received data.');
return;
}
options?.onData?.call(data);
},
onDone: () async { onDone: () async {
await _subscription.cancel(); await _subscription.cancel();
options?.onDispose?.call(); options?.onDispose?.call();
}, },
); );
}
@override onDispose(() async {
Future<void> dispose() async { if (_ws.readyState != io.WebSocket.closed) {
if (_ws.readyState != io.WebSocket.closed) { await _ws.close();
await _ws.close(); }
} });
} }
@override @override
+13 -8
View File
@@ -2,6 +2,8 @@ import 'dart:async';
import 'dart:html' as html; import 'dart:html' as html;
import 'dart:typed_data'; import 'dart:typed_data';
import '../../extensions.dart';
import '../../logger.dart';
import '../websocket.dart'; import '../websocket.dart';
// ignore: avoid_web_libraries_in_flutter // ignore: avoid_web_libraries_in_flutter
@@ -12,7 +14,7 @@ Future<LiveKitWebSocketWeb> lkWebSocketConnect(
]) => ]) =>
LiveKitWebSocketWeb.connect(uri, options); LiveKitWebSocketWeb.connect(uri, options);
class LiveKitWebSocketWeb implements LiveKitWebSocket { class LiveKitWebSocketWeb extends LiveKitWebSocket {
final html.WebSocket _ws; final html.WebSocket _ws;
final WebSocketEventHandlers? options; final WebSocketEventHandlers? options;
late final StreamSubscription _messageSubscription; late final StreamSubscription _messageSubscription;
@@ -24,6 +26,10 @@ class LiveKitWebSocketWeb implements LiveKitWebSocket {
]) { ]) {
_ws.binaryType = 'arraybuffer'; _ws.binaryType = 'arraybuffer';
_messageSubscription = _ws.onMessage.listen((_) { _messageSubscription = _ws.onMessage.listen((_) {
if (isDisposed) {
logger.warning('$objectId already disposed, ignoring received data.');
return;
}
dynamic _data = _.data is ByteBuffer ? _.data.asUint8List() : _.data; dynamic _data = _.data is ByteBuffer ? _.data.asUint8List() : _.data;
options?.onData?.call(_data); options?.onData?.call(_data);
}); });
@@ -32,18 +38,17 @@ class LiveKitWebSocketWeb implements LiveKitWebSocket {
await _closeSubscription.cancel(); await _closeSubscription.cancel();
options?.onDispose?.call(); options?.onDispose?.call();
}); });
onDispose(() async {
if (_ws.readyState != html.WebSocket.CLOSED) {
_ws.close();
}
});
} }
@override @override
void send(List<int> data) => _ws.send(data); void send(List<int> data) => _ws.send(data);
@override
Future<void> dispose() async {
if (_ws.readyState != html.WebSocket.CLOSED) {
_ws.close();
}
}
static Future<LiveKitWebSocketWeb> connect( static Future<LiveKitWebSocketWeb> connect(
Uri uri, [ Uri uri, [
WebSocketEventHandlers? options, WebSocketEventHandlers? options,