platform/packages/websocket/lib/angel_websocket.dart
thomashii@dukefirehawk.com 41188b3df8 Migrated websocket
2021-04-26 08:47:32 +08:00

45 lines
1.2 KiB
Dart

/// WebSocket plugin for Angel.
library angel_websocket;
/// A notification from the server that something has occurred.
class WebSocketEvent<Data> {
String? eventName;
Data? data;
WebSocketEvent({this.eventName, this.data});
factory WebSocketEvent.fromJson(Map data) => WebSocketEvent(
eventName: data['eventName'].toString(), data: data['data'] as Data?);
WebSocketEvent<T> cast<T>() {
if (T == Data) {
return this as WebSocketEvent<T>;
} else {
return WebSocketEvent<T>(eventName: eventName, data: data as T?);
}
}
Map<String, dynamic> toJson() {
return {'eventName': eventName, 'data': data};
}
}
/// A command sent to the server, usually corresponding to a service method.
class WebSocketAction {
String? id;
String? eventName;
var data;
Map<String, dynamic>? params;
WebSocketAction({this.id, this.eventName, this.data, this.params});
factory WebSocketAction.fromJson(Map data) => WebSocketAction(
id: data['id'].toString(),
eventName: data['eventName'].toString(),
data: data['data'],
params: data['params'] as Map<String, dynamic>?);
Map<String, dynamic> toJson() {
return {'id': id, 'eventName': eventName, 'data': data, 'params': params};
}
}