engineConnect method

Future<void> engineConnect(
  1. String token,
  2. int timeout, {
  3. required dynamic onResult(
    1. int? code
    ),
})

Implementation

Future<void> engineConnect(String token, int timeout,
    {required Function(int? code) onResult}) async {
  RCIMWrapperPlatform.instance.writeLog('RCKEngineProvider engineConnect', '',
      0, 'connecting to server with token');
  if (Platform.isIOS) {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    await RCIMWrapperPlatform.instance
        .setModuleName('flutterimkit', packageInfo.version);
  }

  await engine?.connect(token, timeout,
      callback: RCIMIWConnectCallback(onConnected: (code, userIdReturn) {
    if (userIdReturn != null) {
      currentUserId = userIdReturn;
    }
    onResult(code);

    // 连接以后设置AppSettings
    _setAppSettings();

    RCIMWrapperPlatform.instance.writeLog(
        'RCIMIWConnectCallback',
        'onConnected',
        code ?? 0,
        'connection successful, userId: $userIdReturn');
  }));
  notifyListeners();

  engine?.onMessageReceived = (message, left, hasPackage, offline) async {
    receiveMessageNotifier.value = message;
    RCIMWrapperPlatform.instance.writeLog('engine?.onMessageReceived',
        'engineConnect', 0, 'onMessageReceived ${message?.messageId}');
    notifyListeners();
  };

  engine?.onConnectionStatusChanged = (status) {
    networkChangeNotifier.value = status;
    if (status == RCIMIWConnectionStatus.connected) {
      resendFailedMessages();
    }
    RCIMWrapperPlatform.instance.writeLog('engine?.onConnectionStatusChanged',
        'engineConnect', 0, 'onConnectionStatusChanged $status');
    notifyListeners();
  };

  engine?.onConversationTopStatusSynced =
      (conversationType, targetId, channelId, isTop) {
    conversationStatus.value = targetId ?? '$isTop';
    RCIMWrapperPlatform.instance.writeLog(
        'engine?.onConversationTopStatusSynced',
        'engineConnect',
        0,
        'onConversationTopStatusSynced $targetId $isTop');
    notifyListeners();
  };

  engine?.onConversationNotificationLevelSynced =
      (conversationType, targetId, channelId, notificationLevel) {
    conversationStatus.value = targetId ?? '$notificationLevel';
    RCIMWrapperPlatform.instance.writeLog(
        'engine?.onConversationNotificationLevelSynced',
        'engineConnect',
        0,
        'onConversationNotificationLevelSynced $targetId $notificationLevel');
    notifyListeners();
  };

  engine?.onRemoteMessageRecalled = (message) {
    recallMessageNotifier.value = message;
    RCIMWrapperPlatform.instance.writeLog('engine?.onRemoteMessageRecalled',
        'engineConnect', 0, 'onRemoteMessageRecalled ${message?.messageId}');
    notifyListeners();
  };

  engine?.onConversationReadStatusSyncMessageReceived =
      (conversationType, targetId, timestamp) {
    engine?.clearUnreadCount(
        conversationType ?? RCIMIWConversationType.invalid,
        targetId ?? '',
        null,
        timestamp ?? 0, callback:
            IRCIMIWClearUnreadCountCallback(onUnreadCountCleared: (code) {
      if (code == 0) {
        readClearTargetId.value = "$targetId${timestamp ?? 0}";
        notifyListeners();
        debugPrint('同步会话阅读状态成功');
      }
      RCIMWrapperPlatform.instance.writeLog(
          'engine?.onConversationReadStatusSyncMessageReceived',
          'engineConnect',
          code ?? 0,
          'onConversationReadStatusSyncMessageReceived $targetId $timestamp');
    }));
  };

  engine?.onSpeechToTextCompleted = (info, messageUId, code) {
    RCIMIWVoiceMessage voiceMessage = RCIMIWVoiceMessage.fromJson({});
    voiceMessage.speechToTextInfo = info;
    voiceMessage.messageUId = messageUId;
    if (code != 0) {
      voiceMessage.speechToTextInfo?.status = RCIMIWSpeechToTextStatus.failed;
    }
    speechToTextMessageNotifier.value = voiceMessage;

    RCIMWrapperPlatform.instance.writeLog(
        'engine?.onSpeechToTextCompleted',
        'engineConnect',
        code ?? 0,
        'onSpeechToTextCompleted $messageUId $code');
    notifyListeners();
  };
}