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.

A string containing the serialized representation. A logger that will be used to log messages that occur during parsing.

Implementation

@override
List<HubMessageBase> parseMessages(Object input, Logger? logger) {
  // Only JsonContent is allowed.
  if (!(input is String)) {
    throw new GeneralError(
        "Invalid input for JSON hub protocol. Expected a string.");
  }

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

  // Parse the messages
  final messages = TextMessageFormat.parse(jsonInput);
  for (var message in messages) {
    final jsonData = json.decode(message);
    final messageType = _getMessageTypeFromJson(jsonData);
    HubMessageBase messageObj;

    switch (messageType) {
      case MessageType.Invocation:
        messageObj = _getInvocationMessageFromJson(jsonData);
        break;
      case MessageType.StreamItem:
        messageObj = _getStreamItemMessageFromJson(jsonData);
        break;
      case MessageType.Completion:
        messageObj = _getCompletionMessageFromJson(jsonData);
        break;
      case MessageType.Ping:
        messageObj = _getPingMessageFromJson(jsonData);
        break;
      case MessageType.Close:
        messageObj = _getCloseMessageFromJson(jsonData);
        break;
      default:
        // Future protocol changes can add message types, old clients can ignore them
        logger?.info("Unknown message type '$messageType' ignored.");
        continue;
    }
    hubMessages.add(messageObj);
  }

  return hubMessages;
}