diff --git a/.gitignore b/.gitignore index 1403b417..b37504a1 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,4 @@ crashlytics.properties crashlytics-build.properties fabric.properties +.dart_tool \ No newline at end of file diff --git a/lib/server.dart b/lib/server.dart index 1b9e9362..17b89085 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -202,16 +202,15 @@ class AngelWebSocket { action.params['query'] is Map && action.params['query']['jwt'] is String) { try { - var auth = socket.request.grab(AngelAuth); + var auth = socket.request.container.make(); var jwt = action.params['query']['jwt'] as String; AuthToken token; token = new AuthToken.validate(jwt, auth.hmac); var user = await auth.deserializer(token.userId); - var req = socket.request; - req - ..inject(AuthToken, req.properties['token'] = token) - ..inject(user.runtimeType, req.properties["user"] = user); + socket.request + ..container.registerSingleton(token) + ..container.registerSingleton(user, as: user.runtimeType as Type); socket.send(EVENT_AUTHENTICATED, {'token': token.serialize(auth.hmac), 'data': user}); } catch (e, st) { @@ -314,10 +313,10 @@ class AngelWebSocket { /// Configures an [Angel] instance to listen for WebSocket connections. Future configureServer(Angel app) async { - app..container.singleton(this); + app..container.registerSingleton(this); if (runtimeType != AngelWebSocket) - app.container.singleton(this, as: AngelWebSocket); + app..container.registerSingleton(this); // Set up services wireAllServices(app); @@ -340,12 +339,10 @@ class AngelWebSocket { _onConnection.add(socket); - socket.request - ..properties['socket'] = socket - ..inject(WebSocketContext, socket); + socket.request.container.registerSingleton(socket); socket.channel.stream.listen( - (data) { + (data) { _onData.add(data); handleData(socket, data); }, @@ -363,15 +360,12 @@ class AngelWebSocket { /// Handles an incoming HTTP request. Future handleRequest(RequestContext req, ResponseContext res) async { - if (req is HttpRequestContextImpl) { - if (!WebSocketTransformer.isUpgradeRequest(req.io)) + if (req is HttpRequestContext && res is HttpResponseContext) { + if (!WebSocketTransformer.isUpgradeRequest(req.rawRequest)) throw new AngelHttpException.badRequest(); - res - ..willCloseItself = true - ..end(); - - var ws = await WebSocketTransformer.upgrade(req.io); + await res.detach(); + var ws = await WebSocketTransformer.upgrade(req.rawRequest); var channel = new IOWebSocketChannel(ws); var socket = new WebSocketContext(channel, req, res); handleClient(socket); diff --git a/lib/websocket_controller.dart b/lib/websocket_controller.dart index 8056458c..7d911fbc 100644 --- a/lib/websocket_controller.dart +++ b/lib/websocket_controller.dart @@ -39,7 +39,8 @@ class WebSocketController extends Controller { @override Future configureServer(Angel app) async { - if (findExpose() != null) await super.configureServer(app); + if (findExpose(app.container.reflector) != null) + await super.configureServer(app); InstanceMirror instanceMirror = reflect(this); ClassMirror classMirror = reflectClass(this.runtimeType); @@ -58,16 +59,16 @@ class WebSocketController extends Controller { }); ws.onConnection.listen((socket) async { - socket.request - ..inject('socket', socket) - ..inject(WebSocketContext, socket); + if (!socket.request.container.has()) { + socket.request.container.registerSingleton(socket); + } await onConnect(socket); socket.onData.listen((data) => onData(data, socket)); socket.onAction.listen((WebSocketAction action) async { - socket.request.inject(WebSocketAction, action); + socket.request.container.registerSingleton(action); try { await onAction(action, socket); @@ -75,7 +76,8 @@ class WebSocketController extends Controller { if (_handlers.containsKey(action.eventName)) { var methodMirror = _handlers[action.eventName]; var fn = instanceMirror.getField(methodMirror.simpleName).reflectee; - return app.runContained(fn as Function, socket.request, socket.response); + return app.runContained( + fn as Function, socket.request, socket.response); } } catch (e, st) { ws.catchError(e, st, socket); diff --git a/pubspec.yaml b/pubspec.yaml index 47f9a01b..5431df6a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,22 +1,20 @@ name: angel_websocket description: WebSocket plugin for Angel. environment: - sdk: ">=1.8.0 <3.0.0" -version: 1.1.2 + sdk: ">=2.0.0-dev <3.0.0" +version: 2.0.0-alpha author: Tobe O homepage: https://github.com/angel-dart/angel_websocket dependencies: - angel_auth: ^1.1.0-alpha - angel_client: ^1.1.0-alpha - angel_framework: ^1.1.0-alpha + angel_auth: ^2.0.0-alpha + angel_client: ^2.0.0-alpha + angel_framework: ^2.0.0-alpha angel_http_exception: ^1.0.0 - dart2_constant: ^1.0.0 http: ^0.11.0 json_god: ^2.0.0-beta merge_map: ^1.0.0 meta: ^1.0.0 - uuid: ^0.5.3 web_socket_channel: ^1.0.0 dev_dependencies: - logging: any - test: "^0.12.15" + logging: ^0.11.0 + test: ^1.0.0 diff --git a/test/controller/io_test.dart b/test/controller/io_test.dart index ac6c74ce..2a0d0d52 100644 --- a/test/controller/io_test.dart +++ b/test/controller/io_test.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'package:angel_container/mirrors.dart'; import 'package:angel_framework/angel_framework.dart' as srv; import 'package:angel_websocket/io.dart' as ws; import 'package:angel_websocket/server.dart' as srv; @@ -15,7 +16,7 @@ main() { String url; setUp(() async { - app = new srv.Angel(); + app = new srv.Angel(reflector: const MirrorsReflector()); http = new srv.AngelHttp(app, useZone: false); websockets = new srv.AngelWebSocket(app) @@ -34,6 +35,8 @@ main() { client = new ws.WebSockets(url); await client.connect(timeout: new Duration(seconds: 3)); + print('Connected'); + client ..onData.listen((data) { print('Received by client: $data'); diff --git a/test/service/common.dart b/test/service/common.dart index 4e633106..b3aa11e4 100644 --- a/test/service/common.dart +++ b/test/service/common.dart @@ -1,5 +1,5 @@ import 'package:angel_framework/angel_framework.dart'; -import 'package:angel_framework/common.dart'; +import 'package:angel_model/angel_model.dart'; import 'package:angel_websocket/base_websocket_client.dart'; import 'package:angel_websocket/server.dart'; import 'package:test/test.dart'; @@ -11,8 +11,8 @@ class Todo extends Model { Todo({String this.text, String this.when}); } -class TodoService extends TypedService { - TodoService() : super(new MapService()) { +class TodoService extends MapService { + TodoService() : super() { configuration['ws:filter'] = (HookedServiceEvent e, WebSocketContext socket) { print('Hello, service filter world!'); return true; @@ -21,10 +21,10 @@ class TodoService extends TypedService { } testIndex(BaseWebSocketClient client) async { - var Todos = client.service('api/todos'); - Todos.index(); + var todoService = client.service('api/todos'); + todoService.index(); - var indexed = await Todos.onIndexed.first; + var indexed = await todoService.onIndexed.first; print('indexed: $indexed'); expect(indexed, isList);