sendMessage method

void sendMessage(
  1. String namespace,
  2. String sourceId,
  3. String destinationId,
  4. Map<String, dynamic> payload,
)

Implementation

void sendMessage(String namespace, String sourceId, String destinationId, Map<String, dynamic> payload) {
  if (payload['requestId'] == null) {
    payload['requestId'] = _requestId;
    _requestId += 1;
  }

  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), bytes.lengthInBytes));
  Uint32List data = Uint32List.fromList(headers.toList()..addAll(bytes.toList()));

  _socket.add(data);
}