This commit is contained in:
thosakwe 2017-04-09 21:45:45 -04:00
parent e42528ef42
commit 019989e3b1
7 changed files with 26 additions and 16 deletions

View file

@ -1,5 +1,5 @@
# angel_websocket
[![1.0.3](https://img.shields.io/badge/pub-1.0.3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket)
[![1.0.4](https://img.shields.io/badge/pub-1.0.4-brightgreen.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.
@ -29,11 +29,11 @@ main() async {
```
Filtering events is easy with services. Just return a `bool`, whether
Filtering events is easy with hooked services. Just return a `bool`, whether
synchronously or asynchronously.
```dart
myService.properties['ws:filter'] = (WebSocketContext socket) async {
myService.properties['ws:filter'] = (HookedServiceEvent e, WebSocketContext socket) async {
return true;
}
```

View file

@ -55,7 +55,7 @@ class WebSockets extends BaseWebSocketClient {
@override
Future<WebSocketChannel> getConnectedWebSocket() {
var socket = new WebSocket(basePath);
var socket = new WebSocket(authToken?.isNotEmpty == true ? basePath : '$basePath?token=$authToken');
var completer = new Completer<WebSocketChannel>();
socket

8
lib/hooks.dart Normal file
View file

@ -0,0 +1,8 @@
import 'package:angel_framework/angel_framework.dart';
/// Prevents a WebSocket event from being broadcasted, to any client.
HookedServiceEventListener doNotBroadcast() {
return (HookedServiceEvent e) {
if (e.params != null) e.params['broadcast'] = false;
};
}

View file

@ -38,7 +38,10 @@ class WebSockets extends BaseWebSocketClient {
@override
Future<WebSocketChannel> getConnectedWebSocket() async {
var socket = await WebSocket.connect(basePath);
var socket = await WebSocket.connect(basePath,
headers: authToken?.isNotEmpty == true
? {'Authorization': 'Bearer $authToken'}
: {});
return new IOWebSocketChannel(socket);
}

View file

@ -87,7 +87,7 @@ class AngelWebSocket extends AngelPlugin {
_filter(WebSocketContext socket) {
if (e.service.properties.containsKey('ws:filter'))
return e.service.properties['ws:filter'](socket);
return e.service.properties['ws:filter'](e, socket);
else
return true;
}
@ -230,14 +230,7 @@ class AngelWebSocket extends AngelPlugin {
/// Hooks a service up to have its events broadcasted.
hookupService(Pattern _path, HookedService service) {
String path = _path.toString();
var batch = serviceHook(path);
service
..afterCreated.listen(batch)
..afterModified.listen(batch)
..afterUpdated.listen(batch)
..afterRemoved.listen(batch);
service.afterAll(serviceHook(path));
_servicesAlreadyWired.add(path);
}

View file

@ -2,7 +2,7 @@ name: angel_websocket
description: WebSocket plugin for Angel.
environment:
sdk: ">=1.19.0"
version: 1.0.3
version: 1.0.4
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_websocket
dependencies:

View file

@ -1,6 +1,7 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/common.dart';
import 'package:angel_websocket/base_websocket_client.dart';
import 'package:angel_websocket/server.dart';
import 'package:test/test.dart';
class Todo extends Model {
@ -11,7 +12,12 @@ class Todo extends Model {
}
class TodoService extends TypedService<Todo> {
TodoService() : super(new MapService());
TodoService() : super(new MapService()) {
properties['ws:filter'] = (HookedServiceEvent e, WebSocketContext socket) {
print('Hello, service filter world!');
return true;
};
}
}
testIndex(BaseWebSocketClient client) async {