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
|
|
|
|
2019-01-06 02:41:46 +00:00
|
|
|
WebSockets(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,
|
2019-01-06 02:41:46 +00:00
|
|
|
{String eventName = 'token', String errorMessage}) {
|
2017-03-02 03:54:58 +00:00
|
|
|
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-11-25 20:58:41 +00:00
|
|
|
StreamSubscription<Event> 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) {
|
2018-07-10 16:54:55 +00:00
|
|
|
ctrl.add((e as CustomEvent).detail.toString());
|
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() {
|
2019-01-06 02:41:46 +00:00
|
|
|
var url = websocketUri;
|
|
|
|
|
|
|
|
if (authToken?.isNotEmpty == true) {
|
|
|
|
url = url.replace(
|
|
|
|
queryParameters: new Map<String, String>.from(url.queryParameters)
|
|
|
|
..['token'] = authToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
var socket = new WebSocket(url.toString());
|
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) {
|
2018-10-02 15:32:06 +00:00
|
|
|
if (!completer.isCompleted)
|
|
|
|
return completer.completeError(e is ErrorEvent ? e.error : e);
|
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
|
2018-10-21 08:15:51 +00:00
|
|
|
BrowserWebSocketsService<Id, Data> service<Id, Data>(String path,
|
|
|
|
{Type type, AngelDeserializer<Data> deserializer}) {
|
2016-12-23 20:57:46 +00:00
|
|
|
String uri = path.replaceAll(_straySlashes, '');
|
2018-10-21 08:15:51 +00:00
|
|
|
return new BrowserWebSocketsService<Id, Data>(socket, this, uri,
|
2017-09-24 04:37:58 +00:00
|
|
|
deserializer: deserializer);
|
2016-07-21 01:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 08:15:51 +00:00
|
|
|
class BrowserWebSocketsService<Id, Data> extends WebSocketsService<Id, Data> {
|
2016-12-23 20:57:46 +00:00
|
|
|
final Type type;
|
2016-07-21 01:36:16 +00:00
|
|
|
|
2018-07-10 16:54:55 +00:00
|
|
|
BrowserWebSocketsService(WebSocketChannel socket, WebSockets app, String uri,
|
2018-10-21 08:15:51 +00:00
|
|
|
{this.type, AngelDeserializer<Data> deserializer})
|
2017-02-12 20:06:54 +00:00
|
|
|
: super(socket, app, uri, deserializer: deserializer);
|
2016-07-21 01:36:16 +00:00
|
|
|
}
|