parseMessages method

  1. @override
List<HubMessageBase> parseMessages(
  1. Object input,
  2. Logger? logger
)
override

Creates an array of {@link @microsoft/signalr.HubMessage} objects from the specified serialized representation.

If transferFormat is 'Text', the input parameter must be a string, otherwise it must be an ArrayBuffer.

input A string (json), or Uint8List (binary) containing the serialized representation. Logger logger A logger that will be used to log messages that occur during parsing.

Implementation

@override
List<HubMessageBase> parseMessages(Object input, Logger? logger) {
  if (!(input is Uint8List)) {
    throw new GeneralError(
        "Invalid input for MessagePack hub protocol. Expected an Uint8List.");
  }

  final binaryInput = input;
  final List<HubMessageBase> hubMessages = [];

  final messages = BinaryMessageFormat.parse(binaryInput);
  if (messages.length == 0) {
    throw new GeneralError("Cannot encode message which is null.");
  }

  for (var message in messages) {
    if (message.length == 0) {
      throw new GeneralError("Cannot encode message which is null.");
    }

    final unpackedData = msgpack.deserialize(message);
    List<dynamic> unpackedList;
    if (unpackedData == null) {
      throw new GeneralError("Cannot encode message which is null.");
    }
    try {
      unpackedList = List<dynamic>.from(unpackedData as List<dynamic>);
    } catch (_) {
      throw new GeneralError("Invalid payload.");
    }
    if (unpackedList.length == 0) {
      throw new GeneralError("Cannot encode message which is null.");
    }
    final messageObj = _parseMessage(unpackedData, logger);
    if (messageObj != null) {
      hubMessages.add(messageObj);
    }
  }
  return hubMessages;
}