decodePayloadAsBinary static method

dynamic decodePayloadAsBinary(
  1. List<int> data, {
  2. bool? binaryType,
  3. required dynamic callback(
    1. dynamic err, [
    2. dynamic foo,
    3. dynamic bar
    ]),
})

Implementation

static decodePayloadAsBinary(List<int> data,
    {bool? binaryType,
    required Function(dynamic err, [dynamic foo, dynamic bar]) callback}) {
  var bufferTail = data;
  var buffers = [];
  int i;

  while (bufferTail.isNotEmpty) {
    var strLen = '';
    var isString = bufferTail[0] == 0;
    for (i = 1;; i++) {
      if (bufferTail[i] == 255) break;
      // 310 = char length of Number.MAX_VALUE
      if (strLen.length > 310) {
        return callback(error, 0, 1);
      }
      strLen += '${bufferTail[i]}';
    }
    bufferTail = bufferTail.skip(strLen.length + 1).toList();

    var msgLength = int.parse(strLen);

    dynamic msg = bufferTail.getRange(1, msgLength + 1);
    if (isString == true) msg = String.fromCharCodes(msg);
    buffers.add(msg);
    bufferTail = bufferTail.skip(msgLength + 1).toList();
  }

  var total = buffers.length;
  for (i = 0; i < total; i++) {
    var buffer = buffers[i];
    callback(decodePacket(buffer, binaryType: binaryType, utf8decode: true),
        i, total);
  }
}