platform/lib/angel_websocket.dart

84 lines
2.2 KiB
Dart
Raw Normal View History

2016-12-23 20:57:46 +00:00
/// WebSocket plugin for Angel.
2016-06-27 00:42:21 +00:00
library angel_websocket;
2017-02-28 14:15:34 +00:00
const String ACTION_AUTHENTICATE = 'authenticate';
2016-12-23 20:57:46 +00:00
const String ACTION_INDEX = 'index';
const String ACTION_READ = 'read';
const String ACTION_CREATE = 'create';
const String ACTION_MODIFY = 'modify';
const String ACTION_UPDATE = 'update';
const String ACTION_REMOVE = 'remove';
2017-02-28 14:15:34 +00:00
const String EVENT_AUTHENTICATED = 'authenticated';
2016-12-23 20:57:46 +00:00
const String EVENT_ERROR = 'error';
const String EVENT_INDEXED = 'indexed';
const String EVENT_READ = 'read';
const String EVENT_CREATED = 'created';
const String EVENT_MODIFIED = 'modified';
const String EVENT_UPDATED = 'updated';
const String EVENT_REMOVED = 'removed';
/// The standard Angel service actions.
const List<String> ACTIONS = const [
ACTION_INDEX,
ACTION_READ,
ACTION_CREATE,
ACTION_MODIFY,
ACTION_UPDATE,
ACTION_REMOVE
];
/// The standard Angel service events.
const List<String> EVENTS = const [
EVENT_INDEXED,
EVENT_READ,
EVENT_CREATED,
EVENT_MODIFIED,
EVENT_UPDATED,
EVENT_REMOVED
];
/// A notification from the server that something has occurred.
2018-10-21 08:15:51 +00:00
class WebSocketEvent<Data> {
2016-06-27 00:42:21 +00:00
String eventName;
2018-10-21 08:15:51 +00:00
Data data;
2016-06-27 00:42:21 +00:00
WebSocketEvent({String this.eventName, this.data});
2016-12-23 20:57:46 +00:00
2018-10-02 15:32:06 +00:00
factory WebSocketEvent.fromJson(Map data) => new WebSocketEvent(
2018-10-21 08:15:51 +00:00
eventName: data['eventName'].toString(), data: data['data'] as Data);
2016-12-23 20:57:46 +00:00
2018-10-21 08:15:51 +00:00
WebSocketEvent<T> cast<T>() {
if (T == Data) {
return this as WebSocketEvent<T>;
} else {
return new WebSocketEvent<T>(eventName: eventName, data: data as T);
}
}
Map<String, dynamic> toJson() {
2016-12-23 20:57:46 +00:00
return {'eventName': eventName, 'data': data};
}
2016-06-27 00:42:21 +00:00
}
2016-12-23 20:57:46 +00:00
/// A command sent to the server, usually corresponding to a service method.
2016-06-27 00:42:21 +00:00
class WebSocketAction {
String id;
String eventName;
var data;
2018-10-02 15:32:06 +00:00
Map<String, dynamic> params;
2016-12-23 20:57:46 +00:00
WebSocketAction(
{String this.id, String this.eventName, this.data, this.params});
factory WebSocketAction.fromJson(Map data) => new WebSocketAction(
2018-07-10 16:54:55 +00:00
id: data['id'].toString(),
eventName: data['eventName'].toString(),
2016-12-23 20:57:46 +00:00
data: data['data'],
2018-10-02 15:32:06 +00:00
params: data['params'] as Map<String, dynamic>);
2016-12-23 20:57:46 +00:00
2018-11-04 02:17:33 +00:00
Map<String, dynamic> toJson() {
2016-12-23 20:57:46 +00:00
return {'id': id, 'eventName': eventName, 'data': data, 'params': params};
}
2016-09-03 12:34:01 +00:00
}