2020-04-18 13:24:57 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2020-04-18 12:14:54 +00:00
|
|
|
/// WebSocket message
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
class WsMessage {
|
|
|
|
final String id;
|
|
|
|
final String title;
|
|
|
|
final dynamic data;
|
|
|
|
|
|
|
|
const WsMessage({
|
2022-03-10 18:39:57 +00:00
|
|
|
required this.id,
|
|
|
|
required this.title,
|
|
|
|
required this.data,
|
2020-04-18 12:14:54 +00:00
|
|
|
}) : assert(id != null),
|
2020-04-18 13:24:57 +00:00
|
|
|
assert(title != null),
|
|
|
|
assert(title.length > 0);
|
2020-04-18 12:14:54 +00:00
|
|
|
|
|
|
|
/// Construct a message from a JSON document (messages coming from the server)
|
|
|
|
static WsMessage fromJSON(final Map<dynamic, dynamic> m) {
|
2021-02-06 12:26:35 +00:00
|
|
|
return WsMessage(id: m["id"] ?? "", title: m["title"], data: m["data"]);
|
2020-04-18 12:14:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 13:24:57 +00:00
|
|
|
/// Turn a message into a JSON object to send it to the API
|
|
|
|
String toJSON() =>
|
|
|
|
jsonEncode({
|
|
|
|
"id": id,
|
|
|
|
"title": title,
|
|
|
|
"data": data,
|
|
|
|
});
|
|
|
|
|
2020-04-18 12:14:54 +00:00
|
|
|
bool get hasId => this.id != null && this.id.isNotEmpty;
|
|
|
|
}
|