nvsmessage.js
7.27 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//获取div的高度
function getHt(){
var all_height=$(window).height();
var div_height = all_height - $("#headFrames").height()-4 ;
$("#message_control").css("height",div_height+"px");
}
var g_groupTopic = '/nvsweb/message/group';
var g_onlineTopic = '/nvsweb/message/online';
var g_userList = null;
var g_groupId = "101";
function getUserList(){
var tempData = getUserListFromCache();
if (tempData) {
//console.log('tempData:'+tempData);
g_userList = JSON.parse(""+tempData);
return true;
}
var xhr = $.ajax({
url: "/nvsthird/getuserlist",
type: "post",
async: true,
dataType: "json",
contentType: "application/json",
data: JSON.stringify({
token: "",
fromindex:0,
toindex:-1
}),
timeout: 5000,
success: function(res) {
if (res.group != null) {
var objToStr = JSON.stringify(res.group);
setUserListToCache(objToStr);
g_userList = res.group;
} else {
layer.msg('获取用户列表失败',{icon: 2, title:'获取列表',time: 2000,skin: 'layer-ext-moon' });
}
},
error: function(XMLHttpRequest, textStatus, errorThrown){
layer.msg('获取用户列表超时:' + errorThrown, {icon: 2, title:'获取列表',time: 2000,skin: 'layer-ext-moon' });
return false;
},
complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
if(status == 'timeout'){//超时,status还有success,error等值的情况
xhr.abort();
} else if(status == 'error') {
xhr.abort();
layer.msg('获取用户列表失败', {icon: 2, time: 2000,title:'获取列表',skin: 'layer-ext-moon' });
}
}
});
}
function getUserObject(id) {
for(var i = 0; i < g_userList.length; i++) {
if(g_userList[i].id === id) {
return g_userList[i];
}
}
return null;
}
function buildFriendList(){
var friendArray = new Array();
var friendListArray = new Array();
var myId = getUserId();
for (var i = 0; i < g_userList.length; i++) {
var uNode = g_userList[i];
if(uNode.id == myId){
//continue;
}
friendListArray.push({
"username":"" + uNode.alias
,"id": "" + uNode.id
,"avatar": "img/qq.jpg"
,"sign": uNode.id
});
}
friendArray.push({
"groupname":"发布公告"
,"id": g_groupId
,"status": "hide"
,"list": friendListArray
});
return friendArray;
}
function startMqtt(layim,myId) {
var randName = Math.floor(Math.random()*(1000000-1))+1;
var tid = "" + myId + randName;
var srvIp = getDefaultSrvIp();
var port = '8083';
var useSSL = false;
if(window.location.protocol === 'https:') {
port = ''+window.location.port;
useSSL = true;
}
mqttX.init({
id:tid,
ip:srvIp,
port:port,
useSSL:useSSL,
success:function(){
console.log("mqttx连接成功");
mqttX.subscribe(g_groupTopic,0);
mqttX.subscribe(g_onlineTopic,0);
},
error:function(){
console.log("mqttx连接失败");
},
connectLost:function(){
console.log("mqttx连接丢失");
},
onMessage:function(message){
var recvTopic = "" + message.destinationName;
console.log("recvTopic:"+recvTopic);
///实时告警主题
if(recvTopic == g_groupTopic) {
var recvMsg = "" + message.payloadString;
var obj = JSON.parse(recvMsg);
var mine = false;
if(obj.mine.id == myId) {
mine = true;
}
console.log("recvMsg:"+recvMsg);
var showMsg = null;
if(obj.to.type == "friend") {
showMsg = {
username: obj.mine.username
,avatar: obj.to.avatar
,id: obj.mine.id
,type: obj.to.type
,content: obj.mine.content
,mine: mine //是否我发送的消息,如果为true,则会显示在右方
,fromid: obj.mine.id //消息的发送者id(比如群组中的某个消息发送者),可用于自动解决浏览器多窗口时的一些问题
};
} else{
showMsg = {
username: obj.mine.username
,avatar: obj.to.avatar
,id: obj.to.id
,type: obj.to.type
,content: obj.mine.content
,mine: mine //是否我发送的消息,如果为true,则会显示在右方
,fromid: obj.mine.id //消息的发送者id(比如群组中的某个消息发送者),可用于自动解决浏览器多窗口时的一些问题
};
}
///组消息或者自己的消息才显示
if(obj.to.id == myId || obj.to.id == g_groupId){
if(!mine){
layim.getMessage(showMsg);
}
}
}
}
});
}
$(function(){
getHt();
layui.use('layer', function(){
var layer = layui.layer;
});
getUserList();
var friendArray = buildFriendList();
var myObj = getUserObject(getUserId());
if(isEmpty(myObj)){
layer.msg('获取用户对象失败',{icon: 2, title:'获取用户',time: 2000,skin: 'layer-ext-moon' });
return;
}
var randName = Math.floor(Math.random()*(1000000-1))+1;
var myId = "" + myObj.id;
console.log('myId:'+myId);
layui.use('layim', function(layim){
//基础配置
layim.config({
init: {
mine: {
"username": "" + myObj.alias //我的昵称
,"id": myId //我的ID
,"status": "online" //在线状态 online:在线、hide:隐身
,"sign": myObj.id //我的签名
,"avatar": "img/qq.jpg" //我的头像
}
,friend: friendArray
,group: [
{
"groupname": "发布公告"
,"id": g_groupId
,"avatar": "img/qq.jpg"
}
]
}
//查看群员接口
/*,members: {
url: '<a href="javascript:buildGroupMembers()"></a>'
,data: {}
}*/
//,msgbox: layui.cache.dir + 'css/modules/layim/html/msgbox.html' //消息盒子页面地址,若不开启,剔除该项即可
,brief: false //是否简约模式(若开启则不显示主面板)
,title: '发布公告' //自定义主面板最小化时的标题
//,right: '100px' //主面板相对浏览器右侧距离
//,minRight: '90px' //聊天面板最小化时相对浏览器右侧距离
//,initSkin: '5.jpg' //1-5 设置初始背景
//,skin: ['aaa.jpg'] //新增皮肤
,isfriend: true //是否开启好友
//,isgroup: false //是否开启群组
,min: false //是否始终最小化主面板,默认false
//,notice: true //是否开启桌面消息提醒,默认false
//,voice: false //声音提醒,默认开启,声音文件为:default.wav
,copyright:false
});
layim.chat({
name: '发布公告'
,type: 'group' //群组类型
,avatar: 'img/qq.jpg'
,id: g_groupId //定义唯一的id方便你处理信息
,members: 0 //成员数,不好获取的话,可以设置为0
});
//监听在线状态的切换事件
layim.on('online', function(data){
//console.log(data);
});
//监听layim建立就绪
//layim.on('ready', function(res){
//监听发送消息
layim.on('sendMessage', function(data){
var objToStr = JSON.stringify(data);
console.log("sendMessage:" +objToStr);
mqttX.sendMessage(g_groupTopic,data);
});
//监听查看群员
layim.on('members', function(data){
console.log(data);
});
//监听聊天窗口的切换
layim.on('chatChange', function(res){
var type = res.data.type;
if(type === 'friend'){
} else if(type === 'group'){
}
});
//监听签名
layim.on('sign', function(value){
console.log(value);
});
startMqtt(layim,myId);
});
});