sendMessage method

void sendMessage(
  1. Map payload
)

Implementation

void sendMessage(Map payload) async {
  payload['requestId'] = _requestId;

  CastMessage castMessage = CastMessage();
  castMessage.protocolVersion = CastMessage_ProtocolVersion.CASTV2_1_0;
  castMessage.sourceId = _sourceId!;
  castMessage.destinationId = _destinationId!;
  castMessage.namespace = _namespace!;
  castMessage.payloadType = CastMessage_PayloadType.STRING;
  castMessage.payloadUtf8 = jsonEncode(payload);

  Uint8List bytes = castMessage.writeToBuffer();
  Uint32List headers = Uint32List.fromList(writeUInt32BE(
      List<int>.filled(4, 0, growable: false), bytes.lengthInBytes));
  Uint32List fullData =
      Uint32List.fromList(headers.toList()..addAll(bytes.toList()));

  log.fine(payload['type']);

  _socket!.add(fullData);

  _requestId++;
}