-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.dart
438 lines (353 loc) · 11.1 KB
/
message.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import 'dart:convert';
import 'message_type.dart';
/// The base class representing a message in mid websocket protocol
///
/// Each [MessageType] has its own [Message] type and in some cases
/// its own payload type (e.g., [SubscribeMessage] with [SubscribePayload])
abstract class Message {
const Message();
String get id;
MessageType get type;
Object? get payload => null;
Map<String, dynamic> toMap() => {
'id': id,
'type': type.name,
'payload': payload,
};
String toJson() => json.encode(toMap());
@override
String toString() {
return 'Message(id: $id, type: $type, payload: $payload)';
}
factory Message.fromMap(Map<String, dynamic> map) {
final type = map['type'];
if (map['type'] == null || map['type'] is! String) {
throw Exception('"Message.fromMap failed due to unkown MessageType -- type $type');
}
final messageType = MessageType.fromName(type);
if (messageType == null) {
throw Exception('"Message.fromMap failed due to unkown MessageType -- type $type');
}
try {
switch (messageType) {
case MessageType.ping:
return PingMessage();
case MessageType.pong:
return PongMessage();
case MessageType.connectionInit:
return ConnectionInitMessage.fromMap(map);
case MessageType.connectionUpdate:
return ConnectionUpdateMessage.fromMap(map);
case MessageType.connectionAcknowledge:
return ConnectionAcknowledgeMessage();
case MessageType.subscribe:
return SubscribeMessage.fromMap(map);
case MessageType.stop:
return StopMessage(id: map['id']);
case MessageType.complete:
return CompleteMessage(id: map['id']);
case MessageType.data:
return DataMessage.fromMap(map);
case MessageType.error:
return ErrorMessage.fromMap(map);
case MessageType.endpoint:
return EndpointMessage.fromMap(map);
}
} catch (e) {
throw Exception('Message.fromMap failed to parse the following map\n$map\ndue to the following error:\n$e');
}
}
factory Message.fromJson(String source) => Message.fromMap(json.decode(source));
}
/* -------------------------------------------------------------------------- */
/* ping pong */
/* -------------------------------------------------------------------------- */
class PingMessage extends Message {
const PingMessage();
@override
String get id => type.name;
@override
final MessageType type = MessageType.ping;
}
class PongMessage extends Message {
const PongMessage();
@override
String get id => type.name;
@override
final MessageType type = MessageType.ping;
}
/* -------------------------------------------------------------------------- */
/* connection */
/* -------------------------------------------------------------------------- */
class ConnectionInitMessage extends Message {
@override
final ConnectionPayload payload;
const ConnectionInitMessage({required this.payload});
@override
String get id => type.name;
@override
MessageType get type => MessageType.connectionInit;
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'type': type.name,
'payload': payload.toMap(),
};
}
factory ConnectionInitMessage.fromMap(Map<String, dynamic> map) {
return ConnectionInitMessage(
payload: ConnectionPayload.fromMap(map['payload']),
);
}
}
class ConnectionUpdateMessage extends Message {
@override
final ConnectionPayload payload;
const ConnectionUpdateMessage({required this.payload});
@override
String get id => type.name;
@override
MessageType get type => MessageType.connectionUpdate;
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'type': type.name,
'payload': payload.toMap(),
};
}
factory ConnectionUpdateMessage.fromMap(Map<String, dynamic> map) {
return ConnectionUpdateMessage(
payload: ConnectionPayload.fromMap(map['payload']),
);
}
}
class ConnectionPayload {
final Map<String, String> headers;
ConnectionPayload({required this.headers});
Map<String, dynamic> toMap() {
return {
'headers': headers,
};
}
factory ConnectionPayload.fromMap(Map<String, dynamic> map) {
return ConnectionPayload(
headers: Map<String, String>.from(map['headers']),
);
}
@override
String toString() => 'ConnectionPayload(headers: $headers)';
}
class ConnectionAcknowledgeMessage extends Message {
const ConnectionAcknowledgeMessage();
@override
String get id => type.name;
@override
MessageType get type => MessageType.connectionAcknowledge;
}
/* -------------------------------------------------------------------------- */
/* error */
/* -------------------------------------------------------------------------- */
/// used when no message id is present
const defaultErrorID = 'error';
const defaultConnectionErrorID = 'connectionError';
class ErrorMessage extends Message {
@override
final String id;
@override
final ErrorPayload payload;
final bool isConnectionError;
const ErrorMessage({
required this.id,
required this.payload,
this.isConnectionError = false,
});
@override
MessageType get type => MessageType.error;
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'type': type.name,
'isConnectionError': isConnectionError,
'payload': payload.toMap(),
};
}
factory ErrorMessage.fromMap(Map<String, dynamic> map) {
return ErrorMessage(
id: map['id'],
isConnectionError: map['isConnectionError'],
payload: ErrorPayload.fromMap(map['payload']),
);
}
}
class ErrorPayload {
// TODO: create a set of error codes and decide which ones
// e.g. 0-100 -> error will be sent to the client and the connection will be terminated
// e.g. 100-200 -> error will be sent to the client without terminating the connection
// we might need to use the websocket standard error codes
// also, look at graphql-ws protocol as a reference.
final int errorCode;
final String errorMessage;
ErrorPayload({
required this.errorCode,
required this.errorMessage,
});
Map<String, dynamic> toMap() {
return {
'errorCode': errorCode,
'errorMessage': errorMessage,
};
}
factory ErrorPayload.fromMap(Map<String, dynamic> map) {
return ErrorPayload(
errorCode: map['errorCode'],
errorMessage: map['errorMessage'],
);
}
@override
String toString() => 'ErrorPayload(errorCode: $errorCode, errorMessage: $errorMessage)';
}
/* -------------------------------------------------------------------------- */
/* subscribe */
/* -------------------------------------------------------------------------- */
class SubscribeMessage extends Message {
const SubscribeMessage({required this.id, required this.payload});
@override
final String id;
@override
final SubscribePayload payload;
@override
MessageType get type => MessageType.subscribe;
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'type': type.name,
'payload': payload.toMap(),
};
}
factory SubscribeMessage.fromMap(Map<String, dynamic> map) {
return SubscribeMessage(
id: map['id'],
payload: SubscribePayload.fromMap(map['payload']),
);
}
}
class SubscribePayload {
final String route;
final Map<String, dynamic> args;
const SubscribePayload({required this.route, required this.args});
Map<String, dynamic> toMap() {
return {
'route': route,
'args': args,
};
}
factory SubscribePayload.fromMap(Map<String, dynamic> map) {
return SubscribePayload(
route: map['route'],
args: Map<String, dynamic>.from(map['args']),
);
}
@override
String toString() => 'SubscribePayload(route: $route, args: $args)';
}
/* -------------------------------------------------------------------------- */
/* stop */
/* -------------------------------------------------------------------------- */
class StopMessage extends Message {
const StopMessage({required this.id});
@override
final String id;
@override
MessageType get type => MessageType.stop;
}
/* -------------------------------------------------------------------------- */
/* complete */
/* -------------------------------------------------------------------------- */
class CompleteMessage extends Message {
const CompleteMessage({required this.id});
@override
final String id;
@override
MessageType get type => MessageType.complete;
}
/* -------------------------------------------------------------------------- */
/* data */
/* -------------------------------------------------------------------------- */
class DataMessage extends Message {
const DataMessage({required this.id, required this.payload});
@override
final String id;
/// The payload must be a json encoded string.
@override
final String payload;
@override
MessageType get type => MessageType.data;
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'type': type.name,
'payload': payload,
};
}
factory DataMessage.fromMap(Map<String, dynamic> map) {
return DataMessage(
id: map['id'],
payload: map['payload'],
);
}
}
/* -------------------------------------------------------------------------- */
/* endpoint */
/* -------------------------------------------------------------------------- */
class EndpointMessage extends Message {
const EndpointMessage({required this.id, required this.payload});
@override
final String id;
@override
final EndpointPayload payload;
@override
MessageType get type => MessageType.endpoint;
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'type': type.name,
'payload': payload.toMap(),
};
}
factory EndpointMessage.fromMap(Map<String, dynamic> map) {
return EndpointMessage(
id: map['id'],
payload: EndpointPayload.fromMap(map['payload']),
);
}
}
class EndpointPayload {
final String route;
final Map<String, dynamic> args;
const EndpointPayload({required this.route, required this.args});
Map<String, dynamic> toMap() {
return {
'route': route,
'args': args,
};
}
factory EndpointPayload.fromMap(Map<String, dynamic> map) {
return EndpointPayload(
route: map['route'],
args: Map<String, dynamic>.from(map['args']),
);
}
@override
String toString() => 'EndpointPayload(route: $route, args: $args)';
}
final reservedMessageIDs = [
...MessageType.values.map((e) => e.name),
defaultErrorID,
defaultConnectionErrorID,
];