decodeControl method

ControlFrame decodeControl(
  1. String text
)

Decodes a JSON envelope text into a ControlFrame.

Implementation

ControlFrame decodeControl(String text) {
  final Object? raw;
  try {
    raw = jsonDecode(text);
  } on FormatException catch (e) {
    throw ProtocolException('Invalid control JSON: ${e.message}');
  }
  if (raw is! Map) {
    throw const ProtocolException('Control frame must be a JSON object');
  }
  final map = raw.cast<String, dynamic>();
  final type = map['t'];
  if (type is! String) {
    throw const ProtocolException("Control frame missing 't'");
  }
  final decoder = _decoders[type];
  if (decoder == null) {
    throw ProtocolException('Unknown control message type: $type');
  }
  final channel = map['c'] is int ? map['c'] as int : null;
  final data = map['d'] is Map
      ? (map['d'] as Map).cast<String, dynamic>()
      : <String, dynamic>{};
  return ControlFrame(decoder(channel, data));
}