This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 495
/
init-muti.js
112 lines (96 loc) · 2.72 KB
/
init-muti.js
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
function initMuti(callbacks, appInfo, modules){
window.RongIM = window.RongIM || {};
RongIM.appInfo = RongIM.appInfo || appInfo;
//缓存消息回调队列
onMessageList = window.onMessageList || [];
onMessageList.push(callbacks.onMessage);
//缓存状态队列
onConnectList = window.onConnectList || [];
onConnectList.push(callbacks.onConnect);
modules = modules || {};
var RongIMLib = modules.RongIMLib || window.RongIMLib;
var RongIMClient = RongIMLib.RongIMClient;
if(RongIM.ready){
callbacks.onReady && callbacks.onReady(RongIM.instance);
callbacks.onConnect && callbacks.onConnect(RongIM.userInfo);
return;
}else{
var appInfo = RongIM.appInfo;
var appKey = appInfo.appKey;
var token = appInfo.token;
var navi = appInfo.navi || "";
var protobuf = modules.protobuf || null;
var config = {};
//私有云
if(navi !== ""){
config.navi = navi;
}
//support protobuf url + module
if(protobuf != null){
config.protobuf = protobuf;
};
RongIMLib.RongIMClient.init(appKey,null,config);
//开始链接
RongIMClient.connect(token, {
onSuccess: function(userId) {
RongIM.ready = true;
RongIM.userInfo = {
data : {userId: userId},
status : "ok",
info : "链接成功"
};
for(var i = 0, len = onConnectList.length; i<len; i++){
onConnectList[i](RongIM.userInfo);
}
},
onTokenIncorrect: function() {
// console.log('token无效');
RongIM.ready = false;
RongIM.userInfo = {
data : {},
status : "fail",
info : "token无效"
};
for(var i = 0, len = onConnectList.length; i<len; i++){
onConnectList[i](userInfo);
}
},
onError:function(errorCode){
// console.log("connect error");
// console.log(errorCode);
RongIM.ready = false;
RongIM.userInfo = {
data : {},
status : "fail",
info : errorCode
};
for(var i = 0, len = onConnectList.length; i<len; i++){
onConnectList[i](userInfo);
}
}
});
}
// 连接状态监听器
RongIMClient.setConnectionStatusListener({
onChanged: function (status) {
// console.log(status);
switch (status) {
case RongIMLib.ConnectionStatus.CONNECTED:
RongIM.instance = RongIMClient.getInstance();
callbacks.onReady && callbacks.onReady(RongIM.instance);
break;
}
}
});
RongIMClient.setOnReceiveMessageListener({
// 接收到的消息
onReceived: function (message) {
// 判断消息类型
// console.log("新消息: " + message.targetId);
// console.log(message);
for(var i = 0, len = onMessageList.length; i<len; i++){
onMessageList[i](message);
}
}
});
}