diff --git a/README.md b/README.md index 2373bcf5..5280a65e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # angel_websocket -[![1.0.2](https://img.shields.io/badge/pub-1.0.2-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket) +[![1.0.3](https://img.shields.io/badge/pub-1.0.3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket) [![build status](https://travis-ci.org/angel-dart/websocket.svg)](https://travis-ci.org/angel-dart/websocket) WebSocket plugin for Angel. diff --git a/lib/base_websocket_client.dart b/lib/base_websocket_client.dart index 6d0eb7f3..e420160d 100644 --- a/lib/base_websocket_client.dart +++ b/lib/base_websocket_client.dart @@ -263,6 +263,16 @@ class BaseWebSocketService extends Service { listen(); } + Future close() async { + _onAllEvents.close(); + _onCreated.close(); + _onIndexed.close(); + _onModified.close(); + _onRead.close(); + _onRemoved.close(); + _onUpdated.close(); + } + /// Serializes an [action] to be sent over a WebSocket. serialize(WebSocketAction action) => JSON.encode(action); diff --git a/lib/browser.dart b/lib/browser.dart index 8affcc95..2cec58d4 100644 --- a/lib/browser.dart +++ b/lib/browser.dart @@ -15,8 +15,44 @@ final RegExp _straySlashes = new RegExp(r"(^/)|(/+$)"); /// Queries an Angel server via WebSockets. class WebSockets extends BaseWebSocketClient { + final List _services = []; + WebSockets(String path) : super(new http.BrowserClient(), path); + @override + Future close() { + for (var service in _services) { + service.close(); + } + + return super.close(); + } + + @override + Stream authenticateViaPopup(String url, + {String eventName: 'token', String errorMessage}) { + var ctrl = new StreamController(); + var wnd = window.open(url, 'angel_client_auth_popup'); + + wnd + ..on['beforeunload'].listen((_) { + if (!ctrl.isClosed) { + ctrl.addError(new AngelHttpException.notAuthenticated( + message: + errorMessage ?? 'Authentication via popup window failed.')); + ctrl.close(); + } + }) + ..on[eventName ?? 'token'].listen((CustomEvent e) { + if (!ctrl.isClosed) { + ctrl.add(e.detail); + ctrl.close(); + } + }); + + return ctrl.stream; + } + @override Future getConnectedWebSocket() { var socket = new WebSocket(basePath); diff --git a/lib/io.dart b/lib/io.dart index 6c111124..428b204b 100644 --- a/lib/io.dart +++ b/lib/io.dart @@ -17,8 +17,25 @@ final RegExp _straySlashes = new RegExp(r"(^/)|(/+$)"); /// Queries an Angel server via WebSockets. class WebSockets extends BaseWebSocketClient { + final List _services = []; + WebSockets(String path) : super(new http.Client(), path); + @override + Stream authenticateViaPopup(String url, {String eventName: 'token'}) { + throw new UnimplementedError( + 'Opening popup windows is not supported in the `dart:io` client.'); + } + + @override + Future close() { + for (var service in _services) { + service.close(); + } + + return super.close(); + } + @override Future getConnectedWebSocket() async { var socket = await WebSocket.connect(basePath); diff --git a/pubspec.yaml b/pubspec.yaml index 5f18af65..d6b2dd65 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: angel_websocket description: WebSocket plugin for Angel. environment: sdk: ">=1.19.0" -version: 1.0.2 +version: 1.0.3 author: Tobe O homepage: https://github.com/angel-dart/angel_websocket dependencies: