diff --git a/README.md b/README.md index 150cb6ee..fad0b8ef 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # angel_websocket -[![1.0.0](https://img.shields.io/badge/pub-1.0.0-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket) +[![1.0.1](https://img.shields.io/badge/pub-1.0.1-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. @@ -22,7 +22,9 @@ import "package:angel_websocket/server.dart"; main() async { var app = new Angel(); - await app.configure(new AngelWebSocket("/ws")); + + // Ensure this runs after all our services are in-place + app.justBeforeStart.add(new AngelWebSocket("/ws")); } ``` diff --git a/lib/server.dart b/lib/server.dart index 032b1c1f..ee5f11e2 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -31,6 +31,10 @@ class AngelWebSocket extends AngelPlugin { final StreamController _onDisconnect = new StreamController.broadcast(); + /// If this is not `true`, then all client-side service parameters will be + /// discarded, other than `params['query']`. + final bool allowClientParams; + /// Include debug information, and send error information across WebSockets. final bool debug; @@ -59,10 +63,16 @@ class AngelWebSocket extends AngelPlugin { /// Fired when a user disconnects. Stream get onDisconnection => _onDisconnect.stream; - AngelWebSocket({this.endpoint: '/ws', this.debug: false, this.register}); + AngelWebSocket( + {this.endpoint: '/ws', + this.debug: false, + this.allowClientParams: false, + this.register}); - _batchEvent(String path) { + serviceHook(String path) { return (HookedServiceEvent e) async { + if (e.params != null && e.params['broadcast'] == false) return; + var event = await transformEvent(e); event.eventName = "$path::${event.eventName}"; @@ -115,6 +125,15 @@ class AngelWebSocket extends AngelPlugin { var actionName = split[1]; + if (action.params is! Map) action.params = {}; + + if (allowClientParams != true) { + if (action.params['query'] is Map) + action.params = {'query': action.params['query']}; + else + action.params = {}; + } + var params = mergeMap([ god.deserializeDatum(action.params), { @@ -165,7 +184,7 @@ class AngelWebSocket extends AngelPlugin { /// Hooks a service up to have its events broadcasted. hookupService(Pattern _path, HookedService service) { String path = _path.toString(); - var batch = _batchEvent(path); + var batch = serviceHook(path); service ..afterCreated.listen(batch) @@ -207,10 +226,6 @@ class AngelWebSocket extends AngelPlugin { if (ACTIONS.contains(split[1])) { var event = handleAction(action, socket); if (event is Future) event = await event; - - if (event is WebSocketEvent) { - batchEvent(event); - } } } } diff --git a/pubspec.yaml b/pubspec.yaml index b92a8a6c..e0f2ca3c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ name: angel_websocket -description: WebSocket plugin for Angel +description: WebSocket plugin for Angel. environment: sdk: ">=1.19.0" -version: 1.0.0 +version: 1.0.1 author: Tobe O homepage: https://github.com/angel-dart/angel_websocket dependencies: diff --git a/test/service/common.dart b/test/service/common.dart index fcd4ff6b..e71f30b5 100644 --- a/test/service/common.dart +++ b/test/service/common.dart @@ -1,9 +1,9 @@ import 'package:angel_framework/angel_framework.dart'; -import 'package:angel_framework/src/defs.dart'; +import 'package:angel_framework/common.dart'; import 'package:angel_websocket/base_websocket_client.dart'; import 'package:test/test.dart'; -class Todo extends MemoryModel { +class Todo extends Model { String text; String when;