serialize method
Serialize a given message, should return a String or a UInt8List. The return type is dynamic, because the socket class takes a dynamic type to send either a string or a binary message.
Implementation
@override
Uint8List serialize(AbstractMessage message) {
if (message is Hello) {
return Uint8List.fromList(cbor.encode(CborValue([
MessageTypes.codeHello,
message.realm,
_serializeDetails(message.details)!
])));
}
if (message is Authenticate) {
return Uint8List.fromList(cbor.encode(CborValue([
MessageTypes.codeAuthenticate,
message.signature ?? '',
message.extra ?? {}
])));
}
if (message is Register) {
return Uint8List.fromList(cbor.encode(CborValue([
MessageTypes.codeRegister,
message.requestId,
_serializeRegisterOptions(message.options),
message.procedure
])));
}
if (message is Unregister) {
return Uint8List.fromList(cbor.encode(CborValue([
MessageTypes.codeUnregister,
message.requestId,
message.registrationId
])));
}
if (message is Call) {
var structuredMessage = [
MessageTypes.codeCall,
message.requestId,
_serializeCallOptions(message.options),
message.procedure,
];
_appendPayloadToList(structuredMessage, message);
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Yield) {
var structuredMessage = [
MessageTypes.codeYield,
message.invocationRequestId,
_serializeYieldOptions(message.options)
];
_appendPayloadToList(structuredMessage, message);
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Invocation) {
// for serializer unit test only
var structuredMessage = [
MessageTypes.codeInvocation,
message.requestId,
message.registrationId,
{}
];
_appendPayloadToList(structuredMessage, message);
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Publish) {
var structuredMessage = [
MessageTypes.codePublish,
message.requestId,
_serializePublish(message.options),
message.topic
];
_appendPayloadToList(structuredMessage, message);
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Event) {
var structuredMessage = [
MessageTypes.codeEvent,
message.subscriptionId,
message.publicationId
];
_appendPayloadToList(structuredMessage, message);
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Subscribe) {
var structuredMessage = [
MessageTypes.codeSubscribe,
message.requestId,
_serializeSubscribeOptions(message.options),
message.topic
];
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Unsubscribe) {
var structuredMessage = [
MessageTypes.codeUnsubscribe,
message.requestId,
message.subscriptionId
];
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Error) {
var structuredMessage = [
MessageTypes.codeError,
message.requestTypeId,
message.requestId,
message.details,
message.error
];
_appendPayloadToList(structuredMessage, message);
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Abort) {
var structuredMessage = [
MessageTypes.codeAbort,
message.message != null ? {'message': message.message!.message} : {},
message.reason
];
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
if (message is Goodbye) {
var structuredMessage = [
MessageTypes.codeGoodbye,
message.message != null
? {'message': message.message!.message ?? ''}
: {},
message.reason
];
return Uint8List.fromList(cbor.encode(CborValue(structuredMessage)));
}
_logger.shout('Could not serialize the message of type: $message');
throw Exception(''); // TODO think of something helpful here...
}