No controllers yet

This commit is contained in:
thosakwe 2016-09-17 16:00:17 -04:00
parent ab9926e1d2
commit fb33a99281
4 changed files with 23 additions and 5 deletions

View file

@ -1,5 +1,13 @@
# angel_websocket # angel_websocket
WebSocket plugin for Angel. Features JWT support. WebSocket plugin for Angel.
This plugin broadcasts events from hooked services via WebSockets.
In addition,
it adds itself to the app's IoC container as `AngelWebSocket`, so that it can be used
in controllers as well.
WebSocket contexts are add to `req.params` as `'socket'`.
# Usage # Usage
@ -51,6 +59,8 @@ class Car extends srv.Model {
main() async { main() async {
Angel app = new WebSocketClient("/ws"); Angel app = new WebSocketClient("/ws");
// Wait for WebSocket connection...
await app.connect();
var Cars = app.service("api/cars", type: Car); var Cars = app.service("api/cars", type: Car);
Cars.onCreated.listen((e) { Cars.onCreated.listen((e) {

View file

@ -12,6 +12,7 @@ class WebSocketClient extends Angel {
WebSocketClient(String wsEndpoint) : super(wsEndpoint) { WebSocketClient(String wsEndpoint) : super(wsEndpoint) {
_socket = new WebSocket(wsEndpoint); _socket = new WebSocket(wsEndpoint);
_connect();
} }
onData(data) { onData(data) {
@ -60,7 +61,7 @@ class WebSocketClient extends Angel {
} }
} }
Future connect() async { void _connect() {
_socket.onMessage.listen((MessageEvent event) { _socket.onMessage.listen((MessageEvent event) {
onData(event.data); onData(event.data);
}); });

View file

@ -17,9 +17,10 @@ class Realtime {
const Realtime(); const Realtime();
} }
class AngelWebSocket { class AngelWebSocket extends AngelPlugin {
Angel _app; Angel _app;
List<WebSocket> _clients = []; List<WebSocket> _clients = [];
List<WebSocket> get clients => new List.from(_clients, growable: false);
List<String> servicesAlreadyWired = []; List<String> servicesAlreadyWired = [];
String endpoint; String endpoint;
@ -153,8 +154,12 @@ class AngelWebSocket {
} }
} }
@override
Future call(Angel app) async { Future call(Angel app) async {
this._app = app; this._app = app..container.singleton(this);
if (runtimeType != AngelWebSocket)
app.container.singleton(this, as: AngelWebSocket);
// Set up services // Set up services
wireAllServices(app); wireAllServices(app);
@ -177,6 +182,8 @@ class AngelWebSocket {
var socket = new WebSocketContext(ws, req, res); var socket = new WebSocketContext(ws, req, res);
await onConnect(socket); await onConnect(socket);
req.params['socket'] = socket;
ws.listen((data) { ws.listen((data) {
onData(socket, data); onData(socket, data);
}, onDone: () { }, onDone: () {

View file

@ -1,7 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/defs.dart'; import 'package:angel_framework/src/defs.dart';
class Todo extends MemoryModel { class Todo extends MemoryModel {
String text; String text;