1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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);
            }
        }
 
    }
}