Login

功能介绍

登陆

用户登录腾讯后台服务器后才能正常收发消息,登录需要用户提供UserID、UserSig等信息,具体含义请参考登录鉴权

您需要在以下场景调用 login 接口:

App 启动后首次使用 IM SDK 的功能。

登录时票据过期:login 接口的回调会返回 ERR_USER_SIG_EXPIRED(6206)ERR_SVR_ACCOUNT_USERSIG_EXPIRED(70001) 错误码,此时请您生成新的 userSig 重新登录。

在线时票据过期:用户在线期间也可能收到 UserSigExpiredCallback 回调,此时需要您生成新的 userSig 并重新登录。

在线时被踢下线:用户在线情况下被踢,IM SDK 会通过 KickedOfflineCallback 回调通知给您,此时可以在 UI 提示用户,并调用 login 重新登录。

以下场景无需调用 login 接口:

用户的网络断开并重新连接后,不需要调用 login 函数,IM SDK 会自动上线。

当一个登录过程在进行时,不需要进行重复登录。

调用 IM SDK 接口成功登录后,将会开始计算 DAU,请根据业务场景合理调用登录接口,避免出现 DAU 过高的情况。

在一个 App 中,IM SDK 不支持多个帐号同时在线,如果同时登录多个帐号,只有最后登录的帐号在线。

参数详解

重载1

参数名称 参数类型 是否必填 描述
user_id string 用户ID
user_sig string 通过sdk_app_id与secret生成,可参考 https://cloud.tencent.com/document/product/269/32688
callback NullValueCallback 异步回调

重载2

参数名称 参数类型 是否必填 描述
user_id string 用户ID
user_sig string 通过sdk_app_id与secret生成,可参考 https://cloud.tencent.com/document/product/269/32688
callback ValueCallback< string > 异步回调 返回值的json_param的格式为string

返回模板

TIMResult

调用成功后回调函数参数:
code:int
desc:string
json_param:'' // json_params为空字符串""
user_data:string

返回值详解

名称 数值类型 描述
TIMResult TIMResult 调用接口的返回值
code int 值为ERR_SUCC表示成功,其他值表示失败。详情请参考 错误码
desc string 错误描述字符串
json_param string Json字符串,不同的接口,Json字符串不一样
user_data string ImSDK负责透传的用户自定义数据,未做任何处理

代码案例

using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using com.tencent.im.unity.demo.utils;

public class Login : MonoBehaviour
{
  public Text Header;
  public Text Result;
  public Button Submit;
  public Button Copy;

  void Start()
  {
    Result = GameObject.Find("ResultText").GetComponent<Text>();
    Header = GameObject.Find("HeaderText").GetComponent<Text>();
    Submit = GameObject.Find("Submit").GetComponent<Button>();
    if (CurrentSceneInfo.info != null)
    {
      Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
      Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
    }
    Copy = GameObject.Find("Copy").GetComponent<Button>();
    Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
    Submit.onClick.AddListener(LoginSDK);
    Copy.onClick.AddListener(CopyText);
  }
  void LoginSDK()
  {
    var userid = PlayerPrefs.GetString("UserID", "");
    var user_sig = PlayerPrefs.GetString("Secret", "");
    TIMResult res = TencentIMSDK.Login(userid, user_sig, Utils.addAsyncNullDataToScreen(GetResult));
    Result.text = Utils.SynchronizeResult(res);
  }

  void GetResult(params object[] parameters)
  {
    Result.text += (string)parameters[0];
  }

  void CopyText()
  {
    Utils.Copy(Result.text);
  }
  void OnApplicationQuit()
  {
    TencentIMSDK.Uninit();
  }
}

results matching ""

    No results matching ""