This commit is contained in:
thosakwe 2017-04-23 14:40:30 -04:00
parent 3b7f2ae5c2
commit 63daca458d
6 changed files with 65 additions and 34 deletions

View file

@ -1,5 +1,5 @@
# angel_websocket # angel_websocket
[![1.0.5](https://img.shields.io/badge/pub-1.0.5+3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket) [![1.0.6](https://img.shields.io/badge/pub-1.0.6-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) [![build status](https://travis-ci.org/angel-dart/websocket.svg)](https://travis-ci.org/angel-dart/websocket)
WebSocket plugin for Angel. WebSocket plugin for Angel.

View file

@ -34,18 +34,24 @@ class WebSockets extends BaseWebSocketClient {
var ctrl = new StreamController<String>(); var ctrl = new StreamController<String>();
var wnd = window.open(url, 'angel_client_auth_popup'); var wnd = window.open(url, 'angel_client_auth_popup');
wnd Timer t;
..on['beforeunload'].listen((_) { t = new Timer.periodic(new Duration(milliseconds: 500), (timer) {
if (!ctrl.isClosed) { if (!ctrl.isClosed) {
if (wnd.closed) {
ctrl.addError(new AngelHttpException.notAuthenticated( ctrl.addError(new AngelHttpException.notAuthenticated(
message: message:
errorMessage ?? 'Authentication via popup window failed.')); errorMessage ?? 'Authentication via popup window failed.'));
ctrl.close(); ctrl.close();
timer.cancel();
} }
}) } else
..on[eventName ?? 'token'].listen((CustomEvent e) { timer.cancel();
});
window.on[eventName ?? 'token'].listen((CustomEvent e) {
if (!ctrl.isClosed) { if (!ctrl.isClosed) {
ctrl.add(e.detail); ctrl.add(e.detail);
t.cancel();
ctrl.close(); ctrl.close();
} }
}); });

View file

@ -3,7 +3,8 @@ import 'package:angel_framework/angel_framework.dart';
/// Prevents a WebSocket event from being broadcasted, to any client from the given [provider]. /// Prevents a WebSocket event from being broadcasted, to any client from the given [provider].
/// ///
/// [provider] can be a String, a [Provider], or an Iterable. /// [provider] can be a String, a [Provider], or an Iterable.
HookedServiceEventListener doNotBroadcast(provider) { /// If [provider] is `null`, any provider will be blocked.
HookedServiceEventListener doNotBroadcast([provider]) {
return (HookedServiceEvent e) { return (HookedServiceEvent e) {
if (e.params != null && e.params.containsKey('provider')) { if (e.params != null && e.params.containsKey('provider')) {
bool deny = false; bool deny = false;
@ -16,6 +17,8 @@ HookedServiceEventListener doNotBroadcast(provider) {
deny = deny || deny = deny ||
p == e.params['provider'] || p == e.params['provider'] ||
e.params['provider'] == p.via; e.params['provider'] == p.via;
} else if (p == null) {
deny = true;
} else } else
deny = deny =
deny || (e.params['provider'] as Providers).via == p.toString(); deny || (e.params['provider'] as Providers).via == p.toString();

View file

@ -42,6 +42,11 @@ class AngelWebSocket extends AngelPlugin {
/// Include debug information, and send error information across WebSockets. /// Include debug information, and send error information across WebSockets.
final bool debug; final bool debug;
bool _sendErrors;
/// Send error information across WebSockets, without including [debug] information..
bool get sendErrors => _sendErrors == true;
/// Registers this instance as a route on the server. /// Registers this instance as a route on the server.
final AngelWebSocketRegisterer register; final AngelWebSocketRegisterer register;
@ -70,13 +75,27 @@ class AngelWebSocket extends AngelPlugin {
/// Fired when a user disconnects. /// Fired when a user disconnects.
Stream<WebSocketContext> get onDisconnection => _onDisconnect.stream; Stream<WebSocketContext> get onDisconnection => _onDisconnect.stream;
/// Serializes data to WebSockets.
ResponseSerializer serializer;
/// Deserializes data from WebSockets.
Function deserializer;
AngelWebSocket( AngelWebSocket(
{this.endpoint: '/ws', {this.endpoint: '/ws',
this.debug: false, this.debug: false,
bool sendErrors,
this.allowClientParams: false, this.allowClientParams: false,
this.allowAuth: true, this.allowAuth: true,
this.register, this.register,
this.synchronizer}); this.synchronizer,
this.serializer,
this.deserializer}) {
_sendErrors = sendErrors;
if (serializer == null) serializer = god.serialize;
if (deserializer == null) deserializer = (params) => params;
}
serviceHook(String path) { serviceHook(String path) {
return (HookedServiceEvent e) async { return (HookedServiceEvent e) async {
@ -111,7 +130,7 @@ class AngelWebSocket extends AngelPlugin {
var serialized = event.toJson(); var serialized = event.toJson();
_printDebug('Batching this event: $serialized'); _printDebug('Batching this event: $serialized');
// print('Serialized: ' + JSON.encode(serialized)); // print('Serialized: ' + JSON.encode(serialized));
client.io.add(god.serialize(event.toJson())); client.io.add((serializer ?? god.serialize)(event.toJson()));
} }
}); });
@ -150,7 +169,7 @@ class AngelWebSocket extends AngelPlugin {
} }
var params = mergeMap([ var params = mergeMap([
god.deserializeDatum(action.params), (deserializer ?? (params) => params)(action.params),
{ {
"provider": Providers.WEBSOCKET, "provider": Providers.WEBSOCKET,
'__requestctx': socket.request, '__requestctx': socket.request,
@ -188,7 +207,7 @@ class AngelWebSocket extends AngelPlugin {
} catch (e, st) { } catch (e, st) {
if (e is AngelHttpException) if (e is AngelHttpException)
return socket.sendError(e); return socket.sendError(e);
else if (debug == true) else if (debug == true || _sendErrors == true)
socket.sendError(new AngelHttpException(e, socket.sendError(new AngelHttpException(e,
message: e.toString(), stackTrace: st, errors: [st.toString()])); message: e.toString(), stackTrace: st, errors: [st.toString()]));
else else
@ -218,7 +237,7 @@ class AngelWebSocket extends AngelPlugin {
// Send an error // Send an error
if (e is AngelHttpException) if (e is AngelHttpException)
socket.sendError(e); socket.sendError(e);
else if (debug == true) else if (debug == true || _sendErrors == true)
socket.sendError(new AngelHttpException(e, socket.sendError(new AngelHttpException(e,
message: e.toString(), stackTrace: st, errors: [st.toString()])); message: e.toString(), stackTrace: st, errors: [st.toString()]));
else else
@ -277,7 +296,7 @@ class AngelWebSocket extends AngelPlugin {
// Send an error // Send an error
if (e is AngelHttpException) if (e is AngelHttpException)
socket.sendError(e); socket.sendError(e);
else if (debug == true) else if (debug == true || _sendErrors == true)
socket.sendError(new AngelHttpException(e, socket.sendError(new AngelHttpException(e,
message: e.toString(), stackTrace: st, errors: [st.toString()])); message: e.toString(), stackTrace: st, errors: [st.toString()]));
else else
@ -302,6 +321,8 @@ class AngelWebSocket extends AngelPlugin {
@override @override
Future call(Angel app) async { Future call(Angel app) async {
if (_sendErrors == null) _sendErrors = app.isProduction;
_app = app..container.singleton(this); _app = app..container.singleton(this);
if (runtimeType != AngelWebSocket) if (runtimeType != AngelWebSocket)

View file

@ -68,18 +68,20 @@ class WebSocketController extends Controller {
socket.onAction.listen((WebSocketAction action) async { socket.onAction.listen((WebSocketAction action) async {
socket.request.inject(WebSocketAction, action); socket.request.inject(WebSocketAction, action);
try {
await onAction(action, socket); await onAction(action, socket);
if (_handlers.containsKey(action.eventName)) { if (_handlers.containsKey(action.eventName)) {
try {
var methodMirror = _handlers[action.eventName]; var methodMirror = _handlers[action.eventName];
var fn = instanceMirror.getField(methodMirror.simpleName).reflectee; var fn = instanceMirror.getField(methodMirror.simpleName).reflectee;
return app.runContained(fn, socket.request, socket.response); return app.runContained(fn, socket.request, socket.response);
}
} catch (e, st) { } catch (e, st) {
// Send an error // Send an error
if (e is AngelHttpException) if (e is AngelHttpException)
socket.sendError(e); socket.sendError(e);
else if (ws.debug == true) else if (ws.debug == true || ws.sendErrors == true)
socket.sendError(new AngelHttpException(e, socket.sendError(new AngelHttpException(e,
message: e.toString(), message: e.toString(),
stackTrace: st, stackTrace: st,
@ -87,7 +89,6 @@ class WebSocketController extends Controller {
else else
socket.sendError(new AngelHttpException(e)); socket.sendError(new AngelHttpException(e));
} }
}
}); });
}); });

View file

@ -2,7 +2,7 @@ name: angel_websocket
description: WebSocket plugin for Angel. description: WebSocket plugin for Angel.
environment: environment:
sdk: ">=1.19.0" sdk: ">=1.19.0"
version: 1.0.5 version: 1.0.6
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_websocket homepage: https://github.com/angel-dart/angel_websocket
dependencies: dependencies: