Still needs a LOT of work.

This commit is contained in:
regiostech 2016-06-26 20:42:21 -04:00
parent 4b6ceec17e
commit 098411def7
7 changed files with 198 additions and 230 deletions

View file

@ -12,28 +12,28 @@
<entry key="angel_framework">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-0.0.0-dev.14/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-1.0.0-dev+4pub/lib" />
</list>
</value>
</entry>
<entry key="args">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.4/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.4+2/lib" />
</list>
</value>
</entry>
<entry key="async">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.10.0/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.11.0/lib" />
</list>
</value>
</entry>
<entry key="barback">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+7/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+8/lib" />
</list>
</value>
</entry>
@ -47,7 +47,7 @@
<entry key="boolean_selector">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-1.0.1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-1.0.2/lib" />
</list>
</value>
</entry>
@ -61,35 +61,35 @@
<entry key="collection">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.6.0/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.9.0/lib" />
</list>
</value>
</entry>
<entry key="convert">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-1.1.1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-2.0.0/lib" />
</list>
</value>
</entry>
<entry key="crypto">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-1.1.1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-2.0.1/lib" />
</list>
</value>
</entry>
<entry key="csslib">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.13.1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.13.2/lib" />
</list>
</value>
</entry>
<entry key="glob">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.1.2/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.1.3/lib" />
</list>
</value>
</entry>
@ -103,28 +103,42 @@
<entry key="http">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http-0.11.3+5/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http-0.11.3+7/lib" />
</list>
</value>
</entry>
<entry key="http_multi_server">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_multi_server-2.0.1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_multi_server-2.0.2/lib" />
</list>
</value>
</entry>
<entry key="http_parser">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_parser-2.2.1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_parser-3.0.2/lib" />
</list>
</value>
</entry>
<entry key="json_god">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/json_god-1.0.0/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/json_god-2.0.0-beta/lib" />
</list>
</value>
</entry>
<entry key="json_schema">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/json_schema-1.0.4/lib" />
</list>
</value>
</entry>
<entry key="jwt">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/jwt-0.1.4/lib" />
</list>
</value>
</entry>
@ -159,7 +173,7 @@
<entry key="package_config">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-0.1.3/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-0.1.5/lib" />
</list>
</value>
</entry>
@ -187,28 +201,28 @@
<entry key="pub_semver">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.2.4/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.3.0/lib" />
</list>
</value>
</entry>
<entry key="shelf">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.6.5/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.6.5+2/lib" />
</list>
</value>
</entry>
<entry key="shelf_static">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.3+3/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.3+4/lib" />
</list>
</value>
</entry>
<entry key="shelf_web_socket">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_web_socket-0.2.0/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_web_socket-0.2.1/lib" />
</list>
</value>
</entry>
@ -229,42 +243,42 @@
<entry key="source_span">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_span-1.2.2/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_span-1.2.3/lib" />
</list>
</value>
</entry>
<entry key="stack_trace">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stack_trace-1.6.5/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stack_trace-1.6.6/lib" />
</list>
</value>
</entry>
<entry key="stream_channel">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stream_channel-1.3.1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stream_channel-1.5.0/lib" />
</list>
</value>
</entry>
<entry key="string_scanner">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/string_scanner-0.1.4+1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/string_scanner-1.0.0/lib" />
</list>
</value>
</entry>
<entry key="test">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.13+1/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.14+1/lib" />
</list>
</value>
</entry>
<entry key="typed_data">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.2/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.3/lib" />
</list>
</value>
</entry>
@ -275,6 +289,13 @@
</list>
</value>
</entry>
<entry key="uuid">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/uuid-0.5.3/lib" />
</list>
</value>
</entry>
<entry key="watcher">
<value>
<list>
@ -285,14 +306,14 @@
<entry key="web_socket_channel">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.2/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.4/lib" />
</list>
</value>
</entry>
<entry key="yaml">
<value>
<list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.8/lib" />
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.10/lib" />
</list>
</value>
</entry>
@ -300,47 +321,50 @@
</properties>
<CLASSES>
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/analyzer-0.27.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-0.0.0-dev.14/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.10.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+7/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-1.0.0-dev+4pub/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.4+2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.11.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+8/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/body_parser-1.0.0-dev/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-1.0.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-1.0.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/charcode-1.1.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.6.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-1.1.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-1.1.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.13.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.1.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.9.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-2.0.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-2.0.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.13.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.1.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/html-0.12.2+2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http-0.11.3+5/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_multi_server-2.0.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_parser-2.2.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/json_god-1.0.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http-0.11.3+7/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_multi_server-2.0.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_parser-3.0.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/json_god-2.0.0-beta/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/json_schema-1.0.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/jwt-0.1.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/logging-0.11.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/matcher-0.12.0+2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/merge_map-1.0.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/mime-0.9.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-0.1.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-0.1.5/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/path-1.3.9/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/plugin-0.1.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.2.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.2.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.6.5/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.3+3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_web_socket-0.2.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.3.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.6.5+2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.3+4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_web_socket-0.2.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_map_stack_trace-1.0.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_maps-0.10.1+1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_span-1.2.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stack_trace-1.6.5/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stream_channel-1.3.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/string_scanner-0.1.4+1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.13+1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_span-1.2.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stack_trace-1.6.6/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stream_channel-1.5.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/string_scanner-1.0.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.14+1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/utf-0.9.0+3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/uuid-0.5.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7+2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.8/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.10/lib" />
</CLASSES>
<SOURCES />
</library>

View file

@ -1,2 +1,2 @@
# angel_websocket
WebSocket plugin for Angel.
WebSocket plugin for Angel. Features JWT support.

16
lib/angel_websocket.dart Normal file
View file

@ -0,0 +1,16 @@
library angel_websocket;
class WebSocketEvent {
String id;
String eventName;
var data;
WebSocketEvent({String this.id, String this.eventName, this.data});
}
class WebSocketAction {
String id;
String eventName;
var data;
var params;
}

View file

@ -1,30 +0,0 @@
library angel_websocket.client;
import 'dart:convert' show JSON;
import 'dart:html';
import 'shared.dart';
class Angel {
String wsEndPoint;
WebSocket _socket;
Angel(String this.wsEndPoint) {
_socket = new WebSocket(wsEndPoint);
}
AngelService service(String path) {
return new AngelService._base(_socket, path.trim().replaceAll(new RegExp(r'(^\/+)|(\/+$)'), ''));
}
}
class AngelService {
WebSocket _socket;
String path;
AngelService._base(WebSocket this._socket, path) {}
index([Map params]) {
AngelMessage request = new AngelMessage(path, 'index', body: params);
_socket.send(JSON.encode(request.toMap()));
}
}

View file

@ -1,114 +1,122 @@
///Exposes WebSocket functionality to Angel.
library angel_websocket.server;
import 'dart:async';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'shared.dart';
import 'package:json_god/json_god.dart' as god;
import 'package:uuid/uuid.dart';
import 'angel_websocket.dart';
List<WebSocket> sockets = [];
typedef Future<bool> WebSocketFilter(WebsocketContext context);
_respond(AngelMessage message, Service service, Angel app) async {
if (message.method == 'index') {
return await service.index(message.body['query']);
}
List<WebsocketContext> _clients = [];
Uuid _uuid = new Uuid();
else if (message.method == 'read') {
return await service.read(message.body['id'], message.body['query']);
}
class WebsocketContext {
WebSocket socket;
RequestContext request;
ResponseContext response;
else if (message.method == 'modify') {
return await service.modify(
message.body['id'], message.body['data'] ?? {}, message.body['query']);
}
else if (message.method == 'update') {
await service.update(
message.body['id'], message.body['data'] ?? {}, message.body['query']);
}
else if (message.method == 'remove') {
await service.remove(message.body['id'], message.body['query']);
}
else throw new AngelHttpException.NotImplemented(
message: "This service does not support a \"${message
.method}\" method.");
WebsocketContext(WebSocket this.socket, RequestContext this.request,
ResponseContext this.response);
}
_handleMsg(WebSocket socket, Angel app) {
return (msg) async {
String text = msg.toString();
try {
AngelMessage incoming = new AngelMessage.fromMap(
app.god.serializeToMap(text));
try {
Service service = app.service(incoming.service);
if (service == null) {
throw new AngelHttpException.NotFound(
message: 'The requested service does not exist.');
}
_broadcast(WebSocketEvent event) {
String json = god.serialize(event);
_clients.forEach((WebsocketContext client) {
client.socket.add(json);
});
}
// Now, let's respond. :)
var result = await _respond(incoming, service, app);
AngelMessage response = new AngelMessage(
incoming.service, incoming.method, body: {'result': result});
socket.add(app.god.serialize(response));
} catch (e) {
AngelHttpException err = (e is AngelHttpException)
? e
: new AngelHttpException(e);
AngelMessage response = new AngelMessage(
incoming.service, incoming.method, body: err.toMap());
socket.add(app.god.serialize(response));
_onData(Angel app) {
return (data) {
try {
WebSocketAction action = god.deserialize(
data, outputType: WebSocketAction);
List<String> split = action.eventName.split("::");
if (split.length >= 2) {
Service service = app.service(split[0]);
if (service != null) {
String event = split[1];
if (event == "index") {
}
}
}
} catch (e) {
// If we are sent invalid data, we're not even going to
// bother responding. :)
}
};
}
_wireHooks(bool hookAll) {
return (Angel app) async {
for (Pattern path in app.services.keys) {
Service _service = app.services[path];
_onError(e) {
// Hook any unhooked services
if (!(_service is HookedService) && hookAll) {
app.services[path] = new HookedService(_service);
}
Service service = app.services[path];
if (service is HookedService) {
service.onIndexed.listen((List items) {
});
}
}
};
}
websocket({String endPoint: '/ws', bool hookAll: true}) {
return (Angel app) async {
app.get(endPoint, (RequestContext req, ResponseContext res) async {
if (WebSocketTransformer.isUpgradeRequest(req.underlyingRequest)) {
res
..end()
..willCloseItself = true;
WebSocket socket = await WebSocketTransformer.upgrade(
req.underlyingRequest);
class websocket {
static Map<String, WebSocketFilter> filters = {};
sockets.add(socket);
socket.listen(_handleMsg(socket, app), onDone: () {
// Remove from cache on disconnect
sockets.remove(socket);
call({List<Pattern> endPoints: const['/ws']}) {
return (Angel app) async {
for (Pattern endPoint in endPoints) {
app.all(endPoint, (RequestContext req, ResponseContext res) async {
if (!WebSocketTransformer.isUpgradeRequest(req.underlyingRequest)) {
res.write("This endpoint is only accessible via WebSockets.");
res.end();
} else {
res
..willCloseItself = true
..end();
WebSocket socket = await WebSocketTransformer.upgrade(
req.underlyingRequest);
WebsocketContext context = new WebsocketContext(socket, req, res);
_clients.add(context);
socket.listen(_onData(app), onError: _onError, onDone: () {
_clients.remove(context);
});
}
});
} else {
throw new AngelHttpException.BadRequest(
message: 'This endpoint is only available via WebSockets.');
}
});
await app.configure(_wireHooks(hookAll));
};
app.services.forEach((Pattern path, Service service) {
if (service is HookedService) {
String pathName = (path is RegExp) ? path.pattern : path;
List<HookedServiceEventDispatcher> dispatchers = [
service.afterIndexed,
service.afterCreated,
service.afterRead,
service.afterModified,
service.afterUpdated,
service.afterRemoved
];
for (HookedServiceEventDispatcher dispatcher in dispatchers) {
dispatcher.listen((HookedServiceEvent event) async {
bool canContinue = true;
String filterName = "$pathName::${event.eventName}";
WebSocketFilter filter = filters[filterName];
for (WebsocketContext client in _clients) {
if (filter != null)
canContinue = await filter(client);
}
if (canContinue) {
WebSocketEvent socketEvent = new WebSocketEvent(
id: _uuid.v4(),
eventName: filterName,
data: event.result);
_broadcast(socketEvent);
}
});
}
}
});
}
};
}
}

View file

@ -1,53 +0,0 @@
library angel_websocket;
import 'dart:math';
String _randomString(int length) {
var rand = new Random();
var codeUnits = new List.generate(
length,
(index){
return rand.nextInt(33)+89;
}
);
return new String.fromCharCodes(codeUnits);
}
/// A WebSocket message sent from server to client, or vice-versa.
class AngelMessage {
String id;
String service;
String method;
Map body;
AngelMessage(String this.service, String this.method,
{Map this.body: const {}}) {
id = _randomString(32);
}
/// Parses a Map into an AngelMessage.
AngelMessage.fromMap(Map msg) {
bool invalid = !(msg['service'] is String) ||
(msg['service'] is String && msg['service'].isEmpty);
invalid = invalid || !(msg['method'] is String) ||
(msg['method'] is String && msg['method'].isEmpty);
if (invalid) {
throw new Exception("Invalid message supplied.");
} else {
this.id = _randomString(32);
this.service = msg['service'];
this.method = msg['method'];
this.body = msg['body'] ?? {};
}
}
Map toMap() {
return {
'id': id,
'service': service,
'method': method,
'body': body
};
}
}

View file

@ -1,10 +1,13 @@
name: angel_websocket
description: WebSocket plugin for Angel
version: 0.0.0-dev.0
version: 1.0.0-dev
author: thosakwe <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_websocket
dependencies:
angel_framework: ">=0.0.0-dev < 0.1.0"
angel_framework: ">=1.0.0-dev < 2.0.0"
json_god: ">=2.0.0-beta <3.0.0"
jwt: ">=0.1.4 <1.0.0"
uuid: ">=0.5.3 <1.0.0"
dev_dependencies:
http: ">= 0.11.3 < 0.12.0"
test: ">= 0.12.13 < 0.13.0"