rtmClientEventHandler function

Future<void> rtmClientEventHandler({
  1. required AgoraRtmClient agoraRtmClient,
  2. required AgoraRtmClientEventHandler agoraRtmClientEventHandler,
  3. required SessionController sessionController,
})

Implementation

Future<void> rtmClientEventHandler({
  required AgoraRtmClient agoraRtmClient,
  required AgoraRtmClientEventHandler agoraRtmClientEventHandler,
  required SessionController sessionController,
}) async {
  const String tag = "AgoraVideoUIKit";

  agoraRtmClient.onMessageReceived = (AgoraRtmMessage message, String peerId) {
    agoraRtmClientEventHandler.onMessageReceived?.call(message, peerId);
    Message msg = Message(text: message.text);
    String? messageType;

    message.toJson().forEach((key, val) {
      if (key == "text") {
        var json = jsonDecode(val.toString());
        messageType = json['messageType'];
      }
    });
    messageReceived(
      messageType: messageType!,
      message: msg.toJson(),
      sessionController: sessionController,
    );
  };

  agoraRtmClient.onConnectionStateChanged = (int state, int reason) {
    agoraRtmClientEventHandler.onConnectionStateChanged?.call(state, reason);

    log(
      'Connection state changed : ${state.toString()}, reason : ${reason.toString()}',
      level: Level.info.value,
      name: tag,
    );
    if (state == 5) {
      agoraRtmClient.logout();
    }
  };

  agoraRtmClient.onError = (error) {
    agoraRtmClientEventHandler.onError?.call(error);

    log(
      'Error Occurred while initializing the RTM client: ${error.hashCode}',
      level: Level.error.value,
      name: tag,
    );
  };

  agoraRtmClient.onTokenExpired = () {
    agoraRtmClientEventHandler.onTokenExpired?.call();

    getRtmToken(
      tokenUrl: sessionController.value.connectionData!.tokenUrl,
      sessionController: sessionController,
    );
  };

  agoraRtmClient.onLocalInvitationReceivedByPeer =
      (AgoraRtmLocalInvitation invitation) {
    agoraRtmClientEventHandler.onLocalInvitationReceivedByPeer
        ?.call(invitation);
  };

  agoraRtmClient.onLocalInvitationAccepted =
      (AgoraRtmLocalInvitation invitation) {
    agoraRtmClientEventHandler.onLocalInvitationAccepted?.call(invitation);
  } as Function(LocalInvitation invite, String response)?;

  agoraRtmClient.onLocalInvitationRefused =
      (AgoraRtmLocalInvitation invitation) {
    agoraRtmClientEventHandler.onLocalInvitationRefused?.call(invitation);
  } as void Function(LocalInvitation invite, String response)?;

  agoraRtmClient.onLocalInvitationCanceled =
      (AgoraRtmLocalInvitation invitation) {
    agoraRtmClientEventHandler.onLocalInvitationCanceled?.call(invitation);
  };

  agoraRtmClient.onLocalInvitationFailure =
      (AgoraRtmLocalInvitation invitation, int errorCode) {
    agoraRtmClientEventHandler.onLocalInvitationFailure
        ?.call(invitation, errorCode);
  };

  agoraRtmClient.onRemoteInvitationReceivedByPeer =
      (AgoraRtmRemoteInvitation invitation) {
    agoraRtmClientEventHandler.onRemoteInvitationReceivedByPeer
        ?.call(invitation);
  };

  agoraRtmClient.onRemoteInvitationAccepted =
      (AgoraRtmRemoteInvitation invitation) {
    agoraRtmClientEventHandler.onRemoteInvitationAccepted?.call(invitation);
  };

  agoraRtmClient.onRemoteInvitationRefused =
      (AgoraRtmRemoteInvitation invitation) {
    agoraRtmClientEventHandler.onRemoteInvitationRefused?.call(invitation);
  };

  agoraRtmClient.onRemoteInvitationCanceled =
      (AgoraRtmRemoteInvitation invitation) {
    agoraRtmClientEventHandler.onRemoteInvitationCanceled?.call(invitation);
  };

  agoraRtmClient.onRemoteInvitationFailure =
      (AgoraRtmRemoteInvitation invitation, int errorCode) {
    agoraRtmClientEventHandler.onRemoteInvitationFailure
        ?.call(invitation, errorCode);
  };
}