This commit is contained in:
thosakwe 2017-01-28 16:38:26 -05:00
parent f69c7869c8
commit 64cf7a6534
5 changed files with 28 additions and 14 deletions

View file

@ -1,5 +1,5 @@
# angel_websocket # 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) [![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

@ -14,6 +14,9 @@ export 'angel_websocket.dart';
part 'websocket_context.dart'; part 'websocket_context.dart';
part 'websocket_controller.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. /// Broadcasts events from [HookedService]s, and handles incoming [WebSocketAction]s.
class AngelWebSocket extends AngelPlugin { class AngelWebSocket extends AngelPlugin {
Angel _app; Angel _app;
@ -31,6 +34,9 @@ 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;
/// Registers this instance as a route on the server.
final AngelWebSocketRegisterer register;
/// A list of clients currently connected to this server via WebSockets. /// A list of clients currently connected to this server via WebSockets.
List<WebSocket> get clients => new List.unmodifiable(_clients); List<WebSocket> get clients => new List.unmodifiable(_clients);
@ -53,7 +59,7 @@ 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;
AngelWebSocket({this.endpoint: '/ws', this.debug: false}); AngelWebSocket({this.endpoint: '/ws', this.debug: false, this.register});
_batchEvent(String path) { _batchEvent(String path) {
return (HookedServiceEvent e) async { return (HookedServiceEvent e) async {
@ -79,12 +85,12 @@ class AngelWebSocket extends AngelPlugin {
var split = action.eventName.split("::"); var split = action.eventName.split("::");
if (split.length < 2) if (split.length < 2)
return socket.sendError(new AngelHttpException.BadRequest()); return socket.sendError(new AngelHttpException.badRequest());
var service = _app.service(split[0]); var service = _app.service(split[0]);
if (service == null) if (service == null)
return socket.sendError(new AngelHttpException.NotFound( return socket.sendError(new AngelHttpException.notFound(
message: "No service \"${split[0]}\" exists.")); message: "No service \"${split[0]}\" exists."));
var actionName = split[1]; var actionName = split[1];
@ -118,7 +124,7 @@ class AngelWebSocket extends AngelPlugin {
eventName: "${split[0]}::" + EVENT_REMOVED, eventName: "${split[0]}::" + EVENT_REMOVED,
data: await service.remove(action.id, params)); data: await service.remove(action.id, params));
} else { } else {
return socket.sendError(new AngelHttpException.MethodNotAllowed( return socket.sendError(new AngelHttpException.methodNotAllowed(
message: "Method Not Allowed: \"$actionName\"")); message: "Method Not Allowed: \"$actionName\""));
} }
} catch (e, st) { } catch (e, st) {
@ -160,7 +166,7 @@ class AngelWebSocket extends AngelPlugin {
if (action.eventName == null || if (action.eventName == null ||
action.eventName is! String || action.eventName is! String ||
action.eventName.isEmpty) { action.eventName.isEmpty) {
throw new AngelHttpException.BadRequest(); throw new AngelHttpException.badRequest();
} }
if (fromJson is Map && fromJson.containsKey("eventName")) { if (fromJson is Map && fromJson.containsKey("eventName")) {
@ -225,9 +231,9 @@ class AngelWebSocket extends AngelPlugin {
wireAllServices(app); wireAllServices(app);
}); });
app.get(endpoint, (RequestContext req, ResponseContext res) async { handler(RequestContext req, ResponseContext res) async {
if (!WebSocketTransformer.isUpgradeRequest(req.io)) if (!WebSocketTransformer.isUpgradeRequest(req.io))
throw new AngelHttpException.BadRequest(); throw new AngelHttpException.badRequest();
res res
..willCloseItself = true ..willCloseItself = true
@ -252,6 +258,15 @@ class AngelWebSocket extends AngelPlugin {
_onDisconnect.add(socket); _onDisconnect.add(socket);
_clients.remove(ws); _clients.remove(ws);
}, cancelOnError: true); }, cancelOnError: true);
}); }
_register() {
if (register != null)
return register(app, handler);
else
app.get(endpoint, handler);
}
await _register();
} }
} }

View file

@ -63,7 +63,6 @@ class WebSocketController extends Controller {
try { 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

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.0-dev+6 version: 1.0.0-dev+7
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:

View file

@ -1,5 +1,5 @@
import 'dart:io'; 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_framework/angel_framework.dart' as srv;
import 'package:angel_websocket/io.dart' as ws; import 'package:angel_websocket/io.dart' as ws;
import 'package:angel_websocket/server.dart' as srv; import 'package:angel_websocket/server.dart' as srv;
@ -22,8 +22,8 @@ main() {
}); });
await app.configure(websockets); await app.configure(websockets);
server = await app.configure(logRequests(new File('log.txt')));
await new srv.DiagnosticsServer(app, new File('log.txt')).startServer(); server = await app.startServer();
url = 'ws://${server.address.address}:${server.port}/ws'; url = 'ws://${server.address.address}:${server.port}/ws';
client = new ws.WebSockets(url); client = new ws.WebSockets(url);