From 64cf7a653485be0473cf4f6a608df80c4886c5ad Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 28 Jan 2017 16:38:26 -0500 Subject: [PATCH] +7 --- README.md | 2 +- lib/server.dart | 31 +++++++++++++++++++++++-------- lib/websocket_controller.dart | 1 - pubspec.yaml | 2 +- test/service/io_test.dart | 6 +++--- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f8999930..69cc7b87 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # angel_websocket -[![1.0.0-dev+6](https://img.shields.io/badge/version-1.0.0--dev+6-red.svg)](https://pub.dartlang.org/packages/angel_websocket) +[![1.0.0-dev+7](https://img.shields.io/badge/version-1.0.0--dev+7-red.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/server.dart b/lib/server.dart index f63fcaf5..b82f2dd2 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -14,6 +14,9 @@ export 'angel_websocket.dart'; part 'websocket_context.dart'; part 'websocket_controller.dart'; +/// Used to assign routes to a given handler. +typedef AngelWebSocketRegisterer(Angel app, RequestHandler handler); + /// Broadcasts events from [HookedService]s, and handles incoming [WebSocketAction]s. class AngelWebSocket extends AngelPlugin { Angel _app; @@ -31,6 +34,9 @@ class AngelWebSocket extends AngelPlugin { /// Include debug information, and send error information across WebSockets. final bool debug; + /// Registers this instance as a route on the server. + final AngelWebSocketRegisterer register; + /// A list of clients currently connected to this server via WebSockets. List get clients => new List.unmodifiable(_clients); @@ -53,7 +59,7 @@ class AngelWebSocket extends AngelPlugin { /// Fired when a user disconnects. Stream get onDisconnection => _onDisconnect.stream; - AngelWebSocket({this.endpoint: '/ws', this.debug: false}); + AngelWebSocket({this.endpoint: '/ws', this.debug: false, this.register}); _batchEvent(String path) { return (HookedServiceEvent e) async { @@ -79,12 +85,12 @@ class AngelWebSocket extends AngelPlugin { var split = action.eventName.split("::"); if (split.length < 2) - return socket.sendError(new AngelHttpException.BadRequest()); + return socket.sendError(new AngelHttpException.badRequest()); var service = _app.service(split[0]); if (service == null) - return socket.sendError(new AngelHttpException.NotFound( + return socket.sendError(new AngelHttpException.notFound( message: "No service \"${split[0]}\" exists.")); var actionName = split[1]; @@ -118,7 +124,7 @@ class AngelWebSocket extends AngelPlugin { eventName: "${split[0]}::" + EVENT_REMOVED, data: await service.remove(action.id, params)); } else { - return socket.sendError(new AngelHttpException.MethodNotAllowed( + return socket.sendError(new AngelHttpException.methodNotAllowed( message: "Method Not Allowed: \"$actionName\"")); } } catch (e, st) { @@ -160,7 +166,7 @@ class AngelWebSocket extends AngelPlugin { if (action.eventName == null || action.eventName is! String || action.eventName.isEmpty) { - throw new AngelHttpException.BadRequest(); + throw new AngelHttpException.badRequest(); } if (fromJson is Map && fromJson.containsKey("eventName")) { @@ -225,9 +231,9 @@ class AngelWebSocket extends AngelPlugin { wireAllServices(app); }); - app.get(endpoint, (RequestContext req, ResponseContext res) async { + handler(RequestContext req, ResponseContext res) async { if (!WebSocketTransformer.isUpgradeRequest(req.io)) - throw new AngelHttpException.BadRequest(); + throw new AngelHttpException.badRequest(); res ..willCloseItself = true @@ -252,6 +258,15 @@ class AngelWebSocket extends AngelPlugin { _onDisconnect.add(socket); _clients.remove(ws); }, cancelOnError: true); - }); + } + + _register() { + if (register != null) + return register(app, handler); + else + app.get(endpoint, handler); + } + + await _register(); } } diff --git a/lib/websocket_controller.dart b/lib/websocket_controller.dart index 50ebc0e5..7b3e1361 100644 --- a/lib/websocket_controller.dart +++ b/lib/websocket_controller.dart @@ -63,7 +63,6 @@ class WebSocketController extends Controller { try { var methodMirror = _handlers[action.eventName]; var fn = instanceMirror.getField(methodMirror.simpleName).reflectee; - return app.runContained(fn, socket.request, socket.response); } catch (e, st) { // Send an error diff --git a/pubspec.yaml b/pubspec.yaml index 0390db4f..c3132635 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.0-dev+6 +version: 1.0.0-dev+7 author: Tobe O homepage: https://github.com/angel-dart/angel_websocket dependencies: diff --git a/test/service/io_test.dart b/test/service/io_test.dart index 5b8eb12b..e3d8f9d7 100644 --- a/test/service/io_test.dart +++ b/test/service/io_test.dart @@ -1,5 +1,5 @@ import 'dart:io'; -import 'package:angel_diagnostics/angel_diagnostics.dart' as srv; +import 'package:angel_diagnostics/angel_diagnostics.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; @@ -22,8 +22,8 @@ main() { }); await app.configure(websockets); - server = - await new srv.DiagnosticsServer(app, new File('log.txt')).startServer(); + await app.configure(logRequests(new File('log.txt'))); + server = await app.startServer(); url = 'ws://${server.address.address}:${server.port}/ws'; client = new ws.WebSockets(url);