====== Unity SDK 基础功能 ======
===== EMClient 初始化 =====
EMClient是IM服务的入口,可以直接调用EMClient的接口进行相应地操作。EMClient的初始化操作如下:
void Awake ()
{
EMClient.Instance.Init ();
}
===== 注册、登录及登出 =====
调用EMClient实例的CreateAccount()、Login()、Logout()接口可以实现注册、登录、登出功能。
注册的示例:
regBtn.onClick.AddListener (delegate() {
string nametext = username.text;
string pwdtext = password.text;
int ret = EMClient.Instance.CreateAccount(nametext,pwdtext);
if(ret == 0){
logText.text = "reg success";
}else{
logText.text = "reg error,code="+ret;
}
});
登录的示例:
loginBtn.onClick.AddListener (delegate() {
string nametext = username.text;
string pwdtext = password.text;
EMBaseCallback cb = new EMBaseCallback();
cb.onSuccessCallback = () => {
logText.text = "login success";
SceneManager.LoadScene("MainScene");
};
cb.onProgressCallback = (p,s) => {
logText.text = ("prograss="+p+",status="+s);
};
cb.onErrorCallback = (c,m) => {
logText.text = ("Err code="+c+",msg="+m);
};
EMClient.Instance.Login(nametext,pwdtext,cb);
});
登出的示例:
logoutBtn.onClick.AddListener (delegate() {
EMBaseCallback cb = new EMBaseCallback();
cb.onSuccessCallback = () => {
logText.text = "logout success";
};
cb.onProgressCallback = (p,s) => {
logText.text = ("prograss="+p+",status="+s);
};
cb.onErrorCallback = (c,m) => {
logText.text = ("Err code="+c+",msg="+m);
};
EMClient.Instance.Logout(true,cb);
});
===== 注册连接状态监听 =====
当掉线时,SDK 会自动重连,无需进行任何操作,通过注册连接监听来知道连接状态。
* 在聊天过程中难免会遇到网络问题,在此 SDK 为您提供了网络监听接口,实时监听。
* 可以根据 onDisconnectedCallback 返回的 error 判断原因。
EMConnListenerCallback connCb = new EMConnListenerCallback ();
connCb.onConnectionCallback = () => {
logText.text = "Connected!";
};
connCb.onDisconnectedCallback = (code) => {
logText.text = "Disconnected! code=" + code;
};
EMClient.Instance.connListenerCallback = connCb;
----
上一章节:[[im:unitysdk:sdkintegration|Unity客户端集成]]
下一页:[[im:unitysdk:chat|消息]]