decodePacket static method

dynamic decodePacket(
  1. dynamic data, {
  2. dynamic binaryType,
  3. required bool utf8decode,
})

Implementation

static decodePacket(dynamic data, {binaryType, required bool utf8decode}) {
  dynamic type;

  // String data
  if (data is String) {
    type = data[0];

    if (type == 'b') {
      return decodeBase64Packet((data).substring(1), binaryType);
    }

    if (utf8decode == true) {
      try {
        data = utf8.decode(data.codeUnits);
      } catch (e) {
        return error;
      }
    }
    if ('${int.parse(type)}' != type ||
        packetTypeList[type = int.parse(type)] == null) {
      return error;
    }

    if (data.length > 1) {
      return {'type': packetTypeList[type], 'data': data.substring(1)};
    } else {
      return {'type': packetTypeList[type]};
    }
  }

  // Binary data
  if (binaryType == 'arraybuffer' || data is ByteBuffer) {
    // wrap Buffer/ArrayBuffer data into an Uint8Array
    var intArray = (data as ByteBuffer).asUint8List();
    type = intArray[0];
    return {'type': packetTypeList[type], 'data': intArray.sublist(0)};
  }

//    if (data instanceof ArrayBuffer) {
//      data = arrayBufferToBuffer(data);
//    }
  type = data[0];
  return {'type': packetTypeList[type], 'data': data.sublist(1)};
}