encodeBuffer static method

dynamic encodeBuffer(
  1. dynamic packet,
  2. dynamic supportsBinary,
  3. dynamic callback, {
  4. dynamic fromClient = false,
})

Implementation

static encodeBuffer(packet, supportsBinary, callback, {fromClient = false}) {
  if (!supportsBinary) {
    return encodeBase64Packet(packet, callback);
  }

  var data = packet['data'];
  // 'fromClient' is to check if the runtime is on server side or not,
  // because Dart server's websocket cannot send data with byte buffer.
  final newData = Uint8List(data.length + 1);
  newData
    ..setAll(0, [packetTypeMap[packet['type']]!]..length = 1)
    ..setAll(1, data);
  if (fromClient) {
    return callback(newData.buffer);
  } else {
    return callback(newData);
  }
}