sendUserData function

Future<void> sendUserData({
  1. required bool toChannel,
  2. required String username,
  3. String? peerRtmId,
  4. required SessionController sessionController,
})

Implementation

Future<void> sendUserData({
  required bool toChannel,
  required String username,
  String? peerRtmId,
  required SessionController sessionController,
}) async {
  int ts = DateTime.now().millisecondsSinceEpoch;

  var userData = UserData(
    rtmId: sessionController.value.generatedRtmId!,
    rtcId: sessionController.value.localUid,
    username: username,
    role: sessionController.value.clientRoleType.index,
  );

  var json = jsonEncode(userData);

  Message message = Message(text: json, ts: ts, offline: false);
  RtmMessage msg = RtmMessage.fromText(message.text);

  if (sessionController.value.agoraRtmChannel != null && toChannel) {
    await sessionController.value.agoraRtmChannel?.sendMessage2(msg);
    log('User data sent to channel', level: Level.info.value);
  } else if (sessionController.value.agoraRtmClient != null &&
      !toChannel &&
      peerRtmId != null) {
    await sessionController.value.agoraRtmClient
        ?.sendMessageToPeer2(peerRtmId, msg);
    log('User data sent to peer', level: Level.info.value);
  } else {
    log("No user in the channel", level: Level.warning.value);
  }
}