platform/packages/websocket/lib/angel3_websocket.dart

46 lines
1.2 KiB
Dart
Raw Normal View History

2016-12-23 20:57:46 +00:00
/// WebSocket plugin for Angel.
2021-05-15 07:19:35 +00:00
library angel3_websocket;
2016-06-27 00:42:21 +00:00
2016-12-23 20:57:46 +00:00
/// A notification from the server that something has occurred.
2018-10-21 08:15:51 +00:00
class WebSocketEvent<Data> {
2021-04-26 00:47:32 +00:00
String? eventName;
Data? data;
2016-06-27 00:42:21 +00:00
2021-04-10 15:12:43 +00:00
WebSocketEvent({this.eventName, this.data});
2016-12-23 20:57:46 +00:00
2021-02-21 02:47:23 +00:00
factory WebSocketEvent.fromJson(Map data) => WebSocketEvent(
2021-04-26 00:47:32 +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 {
2021-04-26 00:47:32 +00:00
return WebSocketEvent<T>(eventName: eventName, data: data as T?);
2018-10-21 08:15:51 +00:00
}
}
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 {
2021-04-26 00:47:32 +00:00
String? id;
String? eventName;
2022-02-22 00:07:01 +00:00
dynamic data;
Map<String, dynamic> params;
2022-02-22 00:07:01 +00:00
WebSocketAction({this.id, this.eventName, this.data, this.params = const {}});
2016-12-23 20:57:46 +00:00
2021-02-21 02:47:23 +00:00
factory WebSocketAction.fromJson(Map data) => 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'],
2022-02-22 00:07:01 +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
}