better platform check

This commit is contained in:
Hiroshi Horie
2021-12-15 18:44:01 +07:00
parent 3d5e8a7dd8
commit b24fad0f3b
8 changed files with 50 additions and 14 deletions
+56
View File
@@ -0,0 +1,56 @@
import 'dart:async';
import 'dart:html' as html;
import 'dart:typed_data';
import '../websocket.dart';
// ignore: avoid_web_libraries_in_flutter
Future<LiveKitWebSocketWeb> lkWebSocketConnect(
Uri uri, [
WebSocketEventHandlers? options,
]) =>
LiveKitWebSocketWeb.connect(uri, options);
class LiveKitWebSocketWeb implements LiveKitWebSocket {
final html.WebSocket _ws;
final WebSocketEventHandlers? options;
late final StreamSubscription _messageSubscription;
late final StreamSubscription _closeSubscription;
LiveKitWebSocketWeb._(
this._ws, [
this.options,
]) {
_ws.binaryType = 'arraybuffer';
_messageSubscription = _ws.onMessage.listen((_) {
dynamic _data = _.data is ByteBuffer ? _.data.asUint8List() : _.data;
options?.onData?.call(_data);
});
_closeSubscription = _ws.onClose.listen((_) => dispose());
}
@override
void send(List<int> data) => _ws.send(data);
@override
Future<void> dispose() async {
options?.onDispose?.call();
await _messageSubscription.cancel();
await _closeSubscription.cancel();
_ws.close();
}
static Future<LiveKitWebSocketWeb> connect(
Uri uri, [
WebSocketEventHandlers? options,
]) async {
final completer = Completer<LiveKitWebSocketWeb>();
final ws = html.WebSocket(uri.toString());
ws.onOpen
.listen((_) => completer.complete(LiveKitWebSocketWeb._(ws, options)));
ws.onError
.listen((_) => completer.completeError(WebSocketException.connect()));
return completer.future;
}
}