platform/lib/browser.dart

101 lines
2.9 KiB
Dart
Raw Normal View History

2016-12-23 20:57:46 +00:00
/// Browser WebSocket client library for the Angel framework.
library angel_websocket.browser;
2016-07-21 01:36:16 +00:00
import 'dart:async';
import 'dart:html';
import 'package:angel_client/angel_client.dart';
2017-09-24 04:37:58 +00:00
import 'package:angel_http_exception/angel_http_exception.dart';
2017-02-12 03:06:40 +00:00
import 'package:http/browser_client.dart' as http;
2016-12-23 20:57:46 +00:00
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/html.dart';
import 'base_websocket_client.dart';
export 'angel_websocket.dart';
2016-07-21 01:36:16 +00:00
2016-12-23 20:57:46 +00:00
final RegExp _straySlashes = new RegExp(r"(^/)|(/+$)");
2016-07-21 01:36:16 +00:00
2016-12-23 20:57:46 +00:00
/// Queries an Angel server via WebSockets.
class WebSockets extends BaseWebSocketClient {
2017-06-30 23:09:03 +00:00
final List<BrowserWebSocketsService> _services = [];
2017-03-02 03:54:58 +00:00
2017-02-12 03:06:40 +00:00
WebSockets(String path) : super(new http.BrowserClient(), path);
2016-09-18 01:35:16 +00:00
2017-03-02 03:54:58 +00:00
@override
Future close() {
for (var service in _services) {
service.close();
}
return super.close();
}
@override
Stream<String> authenticateViaPopup(String url,
{String eventName: 'token', String errorMessage}) {
var ctrl = new StreamController<String>();
var wnd = window.open(url, 'angel_client_auth_popup');
2017-04-23 18:40:30 +00:00
Timer t;
2017-06-03 18:13:38 +00:00
StreamSubscription<CustomEvent> sub;
2017-04-23 18:40:30 +00:00
t = new Timer.periodic(new Duration(milliseconds: 500), (timer) {
if (!ctrl.isClosed) {
if (wnd.closed) {
2017-03-02 03:54:58 +00:00
ctrl.addError(new AngelHttpException.notAuthenticated(
message:
2017-09-24 04:37:58 +00:00
errorMessage ?? 'Authentication via popup window failed.'));
2017-03-02 03:54:58 +00:00
ctrl.close();
2017-04-23 18:40:30 +00:00
timer.cancel();
2017-06-03 18:13:38 +00:00
sub?.cancel();
2017-03-02 03:54:58 +00:00
}
2017-04-23 18:40:30 +00:00
} else
timer.cancel();
});
2017-09-24 04:37:58 +00:00
sub = window.on[eventName ?? 'token'].listen((e) {
2017-04-23 18:40:30 +00:00
if (!ctrl.isClosed) {
2017-09-24 04:37:58 +00:00
ctrl.add((e as CustomEvent).detail);
2017-04-23 18:40:30 +00:00
t.cancel();
ctrl.close();
2017-06-03 18:13:38 +00:00
sub.cancel();
2017-04-23 18:40:30 +00:00
}
});
2017-03-02 03:54:58 +00:00
return ctrl.stream;
}
2016-07-21 01:36:16 +00:00
@override
2016-12-23 20:57:46 +00:00
Future<WebSocketChannel> getConnectedWebSocket() {
2017-09-24 04:37:58 +00:00
var socket = new WebSocket(authToken?.isNotEmpty == true
? '$basePath?token=$authToken'
: basePath);
2016-12-23 20:57:46 +00:00
var completer = new Completer<WebSocketChannel>();
2016-07-21 01:36:16 +00:00
2016-12-23 20:57:46 +00:00
socket
..onOpen.listen((_) {
if (!completer.isCompleted)
return completer.complete(new HtmlWebSocketChannel(socket));
})
2017-09-24 04:37:58 +00:00
..onError.listen((e) {
var err = e as ErrorEvent;
if (!completer.isCompleted) return completer.completeError(err.error);
2016-12-23 20:57:46 +00:00
});
2016-07-21 01:36:16 +00:00
2016-12-23 20:57:46 +00:00
return completer.future;
2016-07-21 01:36:16 +00:00
}
@override
2017-09-24 04:37:58 +00:00
BrowserWebSocketsService service(String path,
2016-12-23 20:57:46 +00:00
{Type type, AngelDeserializer deserializer}) {
String uri = path.replaceAll(_straySlashes, '');
2017-09-24 04:37:58 +00:00
return new BrowserWebSocketsService(socket, this, uri,
deserializer: deserializer);
2016-07-21 01:36:16 +00:00
}
}
2017-06-30 23:09:03 +00:00
class BrowserWebSocketsService extends WebSocketsService {
2016-12-23 20:57:46 +00:00
final Type type;
2016-07-21 01:36:16 +00:00
2017-06-30 23:09:03 +00:00
BrowserWebSocketsService(WebSocketChannel socket, Angel app, String uri,
2017-02-12 20:06:54 +00:00
{this.type, AngelDeserializer deserializer})
: super(socket, app, uri, deserializer: deserializer);
2016-07-21 01:36:16 +00:00
}