decodePacket static method
dynamic
decodePacket(
- dynamic encodedPacket,
- dynamic binaryType,
- bool utf8decode
)
Implementation
static decodePacket(dynamic encodedPacket, binaryType, bool utf8decode) {
var type;
// String data
if (encodedPacket is String) {
type = encodedPacket[0];
if (type == 'b') {
return decodeBase64Packet((encodedPacket).substring(1), binaryType);
}
if (utf8decode == true) {
try {
encodedPacket = utf8.decode(encodedPacket.codeUnits);
} catch (e) {
return ERROR_PACKET;
}
}
if ('${int.parse(type)}' != type ||
PacketTypeList[type = int.parse(type)] == null) {
return ERROR_PACKET;
}
if (encodedPacket.length > 1) {
return {'type': PacketTypeList[type], 'data': encodedPacket.substring(1)};
} else {
return {'type': PacketTypeList[type]};
}
}
// Binary data
if (binaryType == 'arraybuffer' || encodedPacket is ByteBuffer) {
// wrap Buffer/ArrayBuffer data into an Uint8Array
var intArray = (encodedPacket as ByteBuffer).asUint8List();
type = intArray[0];
return {'type': PacketTypeList[type], 'data': intArray.sublist(0)};
}
// if (data instanceof ArrayBuffer) {
// data = arrayBufferToBuffer(data);
// }
type = encodedPacket[0];
return {'type': PacketTypeList[type], 'data': encodedPacket.sublist(1)};
}