-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cdc4ff
commit 49f686a
Showing
9 changed files
with
269 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2023 LiveKit, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import '../support/disposable.dart'; | ||
import 'websocket/io.dart' if (dart.library.html) 'websocket/web.dart'; | ||
|
||
class WebSocketException implements Exception { | ||
final int code; | ||
const WebSocketException._(this.code); | ||
|
||
static WebSocketException unknown() => const WebSocketException._(0); | ||
static WebSocketException connect() => const WebSocketException._(1); | ||
|
||
@override | ||
String toString() => { | ||
WebSocketException.unknown(): 'Unknown error', | ||
WebSocketException.connect(): 'Failed to connect', | ||
}[this]!; | ||
} | ||
|
||
typedef WebSocketOnData = Function(dynamic data); | ||
typedef WebSocketOnError = Function(dynamic error); | ||
typedef WebSocketOnDispose = Function(); | ||
|
||
class WebSocketEventHandlers { | ||
final WebSocketOnData? onData; | ||
final WebSocketOnError? onError; | ||
final WebSocketOnDispose? onDispose; | ||
|
||
const WebSocketEventHandlers({ | ||
this.onData, | ||
this.onError, | ||
this.onDispose, | ||
}); | ||
} | ||
|
||
typedef WebSocketConnector = Future<LiveKitWebSocket> Function(Uri uri, | ||
[WebSocketEventHandlers? options]); | ||
|
||
abstract class LiveKitWebSocket extends Disposable { | ||
void send(List<int> data); | ||
|
||
static Future<LiveKitWebSocket> connect(Uri uri, | ||
[WebSocketEventHandlers? options]) => | ||
lkWebSocketConnect(uri, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2023 LiveKit, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import 'dart:async'; | ||
import 'dart:io' as io; | ||
|
||
import '../../extensions.dart'; | ||
import '../../logger.dart'; | ||
import '../websocket.dart'; | ||
|
||
Future<LiveKitWebSocketIO> lkWebSocketConnect( | ||
Uri uri, [ | ||
WebSocketEventHandlers? options, | ||
]) => | ||
LiveKitWebSocketIO.connect(uri, options); | ||
|
||
class LiveKitWebSocketIO extends LiveKitWebSocket { | ||
final io.WebSocket _ws; | ||
final WebSocketEventHandlers? options; | ||
late final StreamSubscription _subscription; | ||
|
||
LiveKitWebSocketIO._( | ||
this._ws, [ | ||
this.options, | ||
]) { | ||
_subscription = _ws.listen( | ||
(dynamic data) { | ||
if (isDisposed) { | ||
logger.warning('$objectId already disposed, ignoring received data.'); | ||
return; | ||
} | ||
options?.onData?.call(data); | ||
}, | ||
onDone: () async { | ||
await _subscription.cancel(); | ||
options?.onDispose?.call(); | ||
}, | ||
); | ||
|
||
onDispose(() async { | ||
if (_ws.readyState != io.WebSocket.closed) { | ||
await _ws.close(); | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
void send(List<int> data) { | ||
if (_ws.readyState != io.WebSocket.open) { | ||
logger.fine('[$objectId] Socket not open (state: ${_ws.readyState})'); | ||
return; | ||
} | ||
|
||
try { | ||
_ws.add(data); | ||
} catch (_) { | ||
logger.fine('[$objectId] send did throw ${_}'); | ||
} | ||
} | ||
|
||
static Future<LiveKitWebSocketIO> connect( | ||
Uri uri, [ | ||
WebSocketEventHandlers? options, | ||
]) async { | ||
logger.fine('[WebSocketIO] Connecting(uri: ${uri.toString()})...'); | ||
try { | ||
final ws = await io.WebSocket.connect(uri.toString()); | ||
logger.fine('[WebSocketIO] Connected'); | ||
return LiveKitWebSocketIO._(ws, options); | ||
} catch (_) { | ||
logger.severe('[WebSocketIO] did throw ${_}'); | ||
throw WebSocketException.connect(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2023 LiveKit, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import 'dart:async'; | ||
import 'dart:html' as html; | ||
import 'dart:typed_data'; | ||
|
||
import '../../extensions.dart'; | ||
import '../../logger.dart'; | ||
import '../websocket.dart'; | ||
|
||
// ignore: avoid_web_libraries_in_flutter | ||
|
||
Future<LiveKitWebSocketWeb> lkWebSocketConnect( | ||
Uri uri, [ | ||
WebSocketEventHandlers? options, | ||
]) => | ||
LiveKitWebSocketWeb.connect(uri, options); | ||
|
||
class LiveKitWebSocketWeb extends 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((_) { | ||
if (isDisposed) { | ||
logger.warning('$objectId already disposed, ignoring received data.'); | ||
return; | ||
} | ||
dynamic data = _.data is ByteBuffer ? _.data.asUint8List() : _.data; | ||
options?.onData?.call(data); | ||
}); | ||
_closeSubscription = _ws.onClose.listen((_) async { | ||
await _messageSubscription.cancel(); | ||
await _closeSubscription.cancel(); | ||
options?.onDispose?.call(); | ||
}); | ||
|
||
onDispose(() async { | ||
if (_ws.readyState != html.WebSocket.CLOSED) { | ||
_ws.close(); | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
void send(List<int> data) => _ws.send(data); | ||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.