doImTencentWebPluginInit function

Future<void> doImTencentWebPluginInit({
  1. int? sdkAppId,
  2. bool initCall = true,
  3. EventChatListenerConfig? eventChatListener,
  4. EventCallListenerConfig? eventCallListener,
  5. String? userId,
  6. String? userSig,
})

执行插件的初始化


  • sdkAppId int imSdkAppId
  • initCall bool 是否同时初始化 音视频通话,默认 true
  • eventChatListener EventChatListenerConfig 聊天事件监听
  • eventCallListener EventCallListenerConfig 音视频通话事件监听
  • userId String 登录 im 的用户 userId
  • userSig String 登录 im 的用户 userSig

应该有以下几个调用场景


1、程序初始化启动的时候,可以仅传入 sdkAppId、initCall、eventChatListener、eventCallListener;仅进行 im 的初始化,不进行登录

2、在条件允许的情况下(比如:登录业务完成后),拿到用户的 userId 和 userSig 之后,再次调用该方法。可以仅传入 userId、userSig;仅进行 im / call 的登录。此时 传入 eventChatListener、eventCallListener 无效

3、都传入,同时进行 im 的初始化,然后再进而执行 im / call 的登录

4、都不传。会从缓存中获取存储的相关信息,拿到后进行 场景3 的执行;拿不到就报错,提示需要初始化参数,这个时候你应该重新按照 场景3 调用一次

注意以及调用方式说明


web 版本

  • 在程序启动的位置(浏览器每次刷新都会执行的位置),调用 场景3
  • 在登录页,登录执行完成的地方,拿到用户登录信息后,调用 场景3
  • 在退出登录的地方,调用 退出doImTencentLogout 方法,将会清除相关的缓存,避免下次刷新浏览器因再次调用 场景4 而逻辑异常
import 'package:im_plugin_tencent_web/im/init.dart';

doImTencentLogout();

必须确保调用场景 2 之前,已调用 场景 1

Implementation

Future<void> doImTencentWebPluginInit({
  int? sdkAppId,
  bool initCall = true,
  EventChatListenerConfig? eventChatListener,
  EventCallListenerConfig? eventCallListener,
  String? userId,
  String? userSig,
}) async {
  if (sdkAppId != null && (userId == null || userSig == null)) {
    // 场景 1
    ImState.setImSdkAppID(sdkAppId);
    ImState.setInitCallInstance(initCall);
    ImState.setChatEventChatListenerConfig(eventChatListener);
    ImState.setCallEventCallListenerConfig(eventCallListener);

    await ImPluginTencentWeb.initSdk(
      sdkAppID: sdkAppId,
      initCall: initCall,
      eventChatListener: eventChatListener,
      eventCallListener: eventCallListener,
    );
  } else if (sdkAppId == null && (userId != null && userSig != null)) {
    // 场景 2
    try {
      await ImPluginTencentWeb.login(userID: userId, userSig: userSig);
    } catch(e) {
      throw Exception(e);
    }
  } else if (sdkAppId != null && userId != null && userSig != null) {
    // 场景 3
    ImState.setImSdkAppID(sdkAppId);
    ImState.setInitCallInstance(initCall);
    ImState.setChatEventChatListenerConfig(eventChatListener);
    ImState.setCallEventCallListenerConfig(eventCallListener);

    await ImPluginTencentWeb.initSdk(
      sdkAppID: sdkAppId,
      initCall: initCall,
      eventChatListener: eventChatListener,
      eventCallListener: eventCallListener,
    );

    try {
      await ImPluginTencentWeb.login(userID: userId, userSig: userSig);
    } catch(e) {
      throw Exception(e);
    }
  } else if (sdkAppId == null && userId == null && userSig == null) {
    // 场景 4
    String? sdkAppId = window.sessionStorage.getItem('imSdkAppID');
    if (sdkAppId != null) {
      ImState.setImSdkAppID(int.parse(sdkAppId));
      ImState.setInitCallInstance(initCall);
      ImState.setChatEventChatListenerConfig(eventChatListener);
      ImState.setCallEventCallListenerConfig(eventCallListener);

      ImLoginInfoEntity? imLoginInfo;
      try {
        String? imLoginInfoStr = window.sessionStorage.getItem('imLoginInfo');
        if (imLoginInfoStr != null) {
          imLoginInfo = ImLoginInfoEntity.fromJson(jsonDecode(imLoginInfoStr));

          await ImPluginTencentWeb.initSdk(
            sdkAppID: int.parse(sdkAppId),
            initCall: initCall,
            eventChatListener: eventChatListener,
            eventCallListener: eventCallListener,
          );
        } else {
          throw Exception('缓存中不存在 imLoginInfo');
        }
      } catch(e) {
        if (kDebugMode) {
          print('解析 imLoginInfo 缓存出错');
        }
        throw Exception('解析 imLoginInfo 缓存出错');
      }

      try {
        await ImPluginTencentWeb.login(userID: imLoginInfo.userId, userSig: imLoginInfo.userSig);
      } catch(e) {
        throw Exception(e);
      }
    } else {
      throw Exception('缓存中不存在 imSdkAppID');
    }
  }
}