package safeluck.drive.evaluation.im;
|
|
import android.util.Log;
|
|
import com.anyun.im_lib.interf.IMSClientInteface;
|
import com.anyun.im_lib.listener.IMSConnectStatusCallback;
|
import com.google.gson.Gson;
|
import com.google.gson.JsonArray;
|
import com.google.gson.JsonObject;
|
import com.google.gson.JsonParser;
|
import com.google.gson.reflect.TypeToken;
|
|
|
import java.lang.reflect.Type;
|
import java.util.Vector;
|
|
|
|
/**
|
* MyApplication2
|
* Created by lzw on 2019/12/12. 16:05:30
|
* 邮箱:632393724@qq.com
|
* All Rights Saved! Chongqing AnYun Tech co. LTD
|
*/
|
public class IMSClientBootstrap {
|
|
private static final String TAG = "IMSClientBootstrap";
|
|
private static final IMSClientBootstrap INSTANCE= new IMSClientBootstrap();
|
private IMSClientInteface imsClient;
|
|
/**标记IMSClientBootstrap是否已经初始化**/
|
private boolean isActive;
|
|
|
private IMSClientBootstrap(){
|
|
}
|
|
public static IMSClientBootstrap getInstance(){
|
return INSTANCE;
|
}
|
|
/**
|
*
|
* @param userId
|
* @param token
|
* @param hosts
|
* @param appStatus
|
*/
|
public synchronized void init(String userId, String token, String hosts, int appStatus, IMSConnectStatusCallback imsConnectStatusListener){
|
if (!isActive){
|
Vector<String> serverUrlList = convertHosts(hosts);
|
if (serverUrlList == null || serverUrlList.size() ==0){
|
Log.i(TAG, "init IMLibClientBootstrap error,ims hosts is null");
|
return;
|
}
|
isActive = true;
|
Log.i(TAG, "init IMLibClientBootstrap ,server="+hosts);
|
if (null != imsClient){
|
imsClient.close();
|
}
|
//初始化IMSClientInteface
|
imsClient = IMSClientFactory.getIMSClient();
|
updateAppStatus(appStatus);
|
imsClient.init(serverUrlList,new IMSEventListener(userId,token),imsConnectStatusListener);
|
}
|
|
}
|
|
|
public boolean isActive(){
|
return isActive;
|
}
|
|
public void updateAppStatus(int appStatus) {
|
if (imsClient == null){
|
return;
|
}
|
imsClient.setAppStatus(appStatus);
|
}
|
|
private Vector<String> convertHosts(String hosts) {
|
Log.i(TAG, "convertHosts: "+hosts);
|
if (hosts != null && hosts.length() > 0) {
|
|
Vector<String> serverUrlList = new Vector<>();
|
JsonArray jsonArray =JsonParser.parseString(hosts).getAsJsonArray();
|
for (int i = 0; i < jsonArray.size(); i++) {
|
JsonObject host = jsonArray.get(i).getAsJsonObject();
|
String hostName = host.get("host").getAsString();
|
int port = host.get("port").getAsInt();
|
|
Log.i(TAG, "convertHosts: hostname="+hostName+" port="+port);
|
serverUrlList.add(hostName+" "+port);
|
}
|
return serverUrlList;
|
|
|
|
}
|
return null;
|
}
|
|
public void sendMessage(byte[] message){
|
if (isActive){
|
imsClient.sendMsg(message);
|
}
|
}
|
|
public void addHeartbeat(int seconds){
|
if (isActive){
|
imsClient.addHeartbeatHandler(seconds);
|
}
|
}
|
|
public void close() {
|
if (isActive){
|
if (null != imsClient){
|
imsClient.close();
|
imsClient = null;
|
}
|
isActive = false;
|
}
|
|
}
|
|
/**
|
* 重置TCP连接
|
* 原来已经init过,传输过host,本方法知识重置连接
|
*/
|
public void resetConnect() {
|
if (isActive){
|
if (imsClient != null){
|
imsClient.resetConnect(false);
|
}
|
}
|
|
}
|
}
|