fromBytes static method

ConnectionCloseFrame fromBytes(
  1. ByteData view,
  2. int offset
)
override

A factory constructor to deserialize a frame from bytes.

Implementation

static ConnectionCloseFrame fromBytes(ByteData view, int offset) {
  int currentOffset = offset;

  // Skip type byte
  currentOffset += 1;

  final errorCode = view.getUint32(currentOffset, Endian.big);
  currentOffset += 4;

  final frameType = view.getUint32(currentOffset, Endian.big);
  currentOffset += 4;

  final reasonLength = view.getUint16(currentOffset, Endian.big);
  currentOffset += 2;

  final reasonBytes = Uint8List.view(
    view.buffer,
    view.offsetInBytes + currentOffset,
    reasonLength
  );
  final reasonPhrase = String.fromCharCodes(reasonBytes);

  return ConnectionCloseFrame(
    errorCode: errorCode,
    frameType: frameType,
    reasonPhrase: reasonPhrase,
  );
}