-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
377 lines (370 loc) · 13.8 KB
/
app.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//======================================================================================
var exp = require('express');
var path= require('path');
var app = exp();
var http= require('http').Server(app);
var ip = require('request-ip');
var Q = require('bluebird');
var bodyParser = require('body-parser');
var sio = require('socket.io')(http, {});
//--------------------------------------------------------------------------------------
var redis = require('./redis');
var mysql = require('./db');
var bkjob = require('./job');
//--------------------------------------------------------------------------------------
var ipAvaExpireTime = 3600*2;
//======================================================================================
app.use(exp.static(path.join(__dirname, '/public')));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.urlencoded());
app.get('/chat', function(req, res) {
var ip_ = ip.getClientIp(req);
res.sendFile(__dirname + '/public/html/chat.html');
});
app.post('/chat', function(req, res) {
var statime = req.body.timestamp;
readHistory(statime).then(function(chats) {
if (chats.length == 0 ) {
return res.status(404).send("");
}
var timestamp = chats[chats.length-1].date;
console.log(chats);
var result = chats.map(function(chat) {
chat.date = new Date(chat.date).pattern('yyyy-MM-dd HH:mm:ss');
//return JSON.parse(chat);
return chat;
});
res.status(200).send(JSON.stringify({
timestamp : timestamp,
chat : result
}));
});
});
//--------------------------------------------------------------------------------------
Date.prototype.pattern=function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
"H+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
var week = {
"0" : "/u65e5",
"1" : "/u4e00",
"2" : "/u4e8c",
"3" : "/u4e09",
"4" : "/u56db",
"5" : "/u4e94",
"6" : "/u516d"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
}
for(var k in o){
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
};
//-----------------------------------------------------------------------------------
/**
* 注册IP地址
*/
var AVATARS = [
'#23c6b1','#e7e2c4','#97c8c2','#ebd79a','#a0e7e1',
'#eeccb1','#bfebee','#f5b1a8','#baf1d2','#fc6471'
];
var AVATAR_SET_KEY = 'AVATAR-SET';
var IP_AVATAR_PAIR_HSET_KEY = 'IP-AVATAR-HSET';
var IP_AVATAR_PAIR_HISTORY_KEY_PREFIX = 'IP-AVATAR-HIST:';
//-----------------------------------------------------------------------------------
/**
* 注册一个IP-AVA对
* @param ip
*/
var onLogin = function(ip) {
//检查IP-AVA中是否有未过期值
var _ =
redis.getAsync(IP_AVATAR_PAIR_HISTORY_KEY_PREFIX+ip).then(function(res) {
//若没有记录,从AVATAR池中获取一个颜色
if (res == null) {
return redis.spopAsync(AVATAR_SET_KEY).then(function(ava) {
ava = ava || "#000000";
return Q.all([
redis.hsetAsync(IP_AVATAR_PAIR_HSET_KEY, [ip, ava]),
redis.setAsync (IP_AVATAR_PAIR_HISTORY_KEY_PREFIX+ip, ava)
]);
});
}
//如果存在,那么将AVATAR池中对应的颜色清除
else {
return Q.all([
redis.delAsync(IP_AVATAR_PAIR_HISTORY_KEY_PREFIX+ip),
redis.setAsync(IP_AVATAR_PAIR_HISTORY_KEY_PREFIX+ip, res),
redis.hsetAsync(IP_AVATAR_PAIR_HSET_KEY, [ip, res]),
redis.sremAsync(AVATAR_SET_KEY, res)
]);
}
});
return _;
};
//-----------------------------------------------------------------------------------
/**
* 保持IP-AVA对直到自动过期
* @param ip
*/
var onLeave = function(ip) {
// 1. 回收ip对应的颜色,放入颜色池
// 2. 删除IP表中的ip
// 3. 设置IP-AVA表中ip过期时间
var _ =
redis.hgetAsync(IP_AVATAR_PAIR_HSET_KEY, ip).then(function(ava) {
var all = [
redis.hdelAsync(IP_AVATAR_PAIR_HSET_KEY,ip),
redis.expireAsync(IP_AVATAR_PAIR_HISTORY_KEY_PREFIX+ip,ipAvaExpireTime)
];
if (ava != '#000000') {
all.push(redis.saddAsync(AVATAR_SET_KEY,ava));
}
return Q.all(all);
});
return _;
}
//-----------------------------------------------------------------------------------
/**
* 根据ip获取头像
* @param ip
*/
var getAvatar = function(ip) {
return redis.hgetAsync(IP_AVATAR_PAIR_HSET_KEY, ip);
}
//-----------------------------------------------------------------------------------
/**
* format : {
* '1': '#23c6b1',
* '9.115.85.54': '#e7e2c4'
* }
*/
var getOnline = function() {
return redis.hgetallAsync(IP_AVATAR_PAIR_HSET_KEY);
};
//-----------------------------------------------------------------------------------
/**
* 存储聊天记录
*/
var CHAT_HASH_KEY_PREFIX = 'CHAT-HASH:';
var CHAT_IDS_ZSET_KEY = 'CHAT-IDS-ZSET';
var CHAT_HASH_KEY_EXPIRE = 2*3600;
var CHAT_EVERY_READ_COUNT = 8;
var chat_ids_read_end = false;
var saveHistory = function(date, json) {
return Q.all([
redis.zaddAsync (CHAT_IDS_ZSET_KEY, date.getTime(), date.getTime()),
redis.hmsetAsync (CHAT_HASH_KEY_PREFIX+date.getTime(), json),
redis.expireAsync(CHAT_HASH_KEY_PREFIX+date.getTime(), CHAT_HASH_KEY_EXPIRE)
]);
};
//-----------------------------------------------------------------------------------
var readChatFromMysql = function(id) {
return bkjob.ChatMysqlHelper.read(id);
};
//-----------------------------------------------------------------------------------
var readChatBatchFromMysql = function(ids) {
return bkjob.ChatMysqlHelper.readBatch(ids);
}
//-----------------------------------------------------------------------------------
// @return Array
var readChatIDsFromMysql = function() {
// 获取目前已经缓存的条数
return redis.zcountAsync(CHAT_IDS_ZSET_KEY, ['-INF', '+INF']).then(function(count) {
return bkjob.ChatMysqlHelper.readIDs(count);
});
};
//-----------------------------------------------------------------------------------
var saveChatToMysql = function(json) {
return bkjob.ChatMysqlHelper.save(json);
};
//-----------------------------------------------------------------------------------
/**
* 读取聊天记录
*/
var readHistory = function(statime) {
// 首先读取聊天记录id列表,获取区间内的id数组
return redis.zrevrangebyscoreAsync(CHAT_IDS_ZSET_KEY, ["("+statime, '-INF', 'LIMIT', 0, CHAT_EVERY_READ_COUNT])
.then(function(ids) {
// 不到预定条数,准备从mysql中读取id列表
if (ids.length < CHAT_EVERY_READ_COUNT) {
if (chat_ids_read_end) {
return Q.resolve(ids);
}
return readChatIDsFromMysql().then(function(ids_from_mysql) {
if (ids_from_mysql.length < CHAT_EVERY_READ_COUNT) {
chat_ids_read_end = true;
}
return Q.map(ids_from_mysql, function(id_from_mysql) {
return redis.zaddAsync(CHAT_IDS_ZSET_KEY, id_from_mysql, id_from_mysql);
}).then(function() {
return redis.zrevrangebyscoreAsync(CHAT_IDS_ZSET_KEY, ["("+statime, '-INF', 'LIMIT', 0, CHAT_EVERY_READ_COUNT]);
});
});
} else {
return Q.resolve(ids);
}
})
.then(function(ids) {
//获取到了聊天id,读取缓存
return Q.map(ids, function(id) {
return redis.hgetallAsync(CHAT_HASH_KEY_PREFIX+id).then(function(chat) {
if (chat == null) {
return Q.resolve({id: id, exist: false});
} else {
// 重新设置过期时间
return redis.expireAsync(CHAT_HASH_KEY_PREFIX+id, CHAT_HASH_KEY_EXPIRE).then(function(err) {
return (err == 1) ? Q.resolve({id: id, exist: true}) : Q.resolve({id: id, exist: false});
});
}
});
});
})
.then(function(packs) {
var exist_ids = [], other_ids = [];
packs.forEach(function(pack) {
( pack.exist ) ? exist_ids.push(pack.id) : other_ids.push(pack.id);
});
if (other_ids.length != 0) {
return readChatBatchFromMysql(other_ids).then(function(chats) {
return Q.map(chats, function(chat) {
return Q.all([
redis.hmsetAsync (CHAT_HASH_KEY_PREFIX+chat.TIME, chat),
redis.expireAsync(CHAT_HASH_KEY_PREFIX+chat.TIME, CHAT_HASH_KEY_EXPIRE)
]);
});
}).then(function(){
return Q.resolve(exist_ids.concat(other_ids).sort().reverse());
});
} else {
return Q.resolve(exist_ids);
}
}).then(function(ids) {
return Q.map(ids, function(id) {
return redis.hgetallAsync(CHAT_HASH_KEY_PREFIX+id).then(function(chat) {
return Q.resolve({
msg : chat.MESSAGE,
ava : chat.AVATAR,
date: parseInt(chat.TIME),
ip : chat.IP
});
});
});
});
};
//-----------------------------------------------------------------------------------
/**
* TODO 未搞清楚机制
* 注册pub-sub监听过期的IP,需要在redis.conf中开启配置生效
var regNofiticationHandler = function() {
//监听notification
redis.psubscribe('__key*__:*');
redis.set('EXPIRE-TEST', 'VALUE');
redis.expire('EXPIRE-TEST', 5);
redis.on('pmessage', function(pattern, channel, message) {
console.log(pattern);
});
};
*/
//-----------------------------------------------------------------------------------
sio.on('connection', function(socket) {
var ip = null;
//有问题,会出现remoteAddress 取不到值的情况。似乎在移动4g网络情况下出现
if (socket.request.connection == null ||
socket.request.connection.remoteAddress == null) {
ip = "0.0.0.0";
} else {
var _ = socket.request.connection.remoteAddress.split(":");
ip = _[_.length-1];
}
onLogin(ip).then(function() {
getOnline().then(function(res) {
sio.emit('PERSONONLINE', JSON.stringify(res));
});
});
socket.on('REQMSG', function(msg) {
getAvatar(ip).then(function(ava) {
var date = new Date();
var json = {
IP : ip,
AVATAR : ava,
MESSAGE : msg,
TIME : date.getTime(),
DATE : date
}
saveHistory(date, json);
saveChatToMysql(json);
sio.emit('RESMSG', JSON.stringify({
msg : msg,
ava : ava,
date: date.pattern('yyyy-MM-dd HH:mm:ss'),
ip : ip
}));
});
});
/**
* 断开连接,断开的时候将颜色返回到颜色池,但是AVA-IP依旧保持一份映射,下次在未超时登录时可以继续取走续约颜色
*/
socket.on('disconnect', function() {
console.log(ip+' disconnected');
onLeave(ip).then(function() {
getOnline().then(function(res) {
console.log(res);
sio.emit('PERSONONLINE', JSON.stringify(res));
});
});
});
});
//--------------------------------------------------------------------------------------
var kis = sio.of('/zsh-zcc');
kis.on('connection', function(socket) {
socket.on('zsh-zcc:txt-msg', function(msg) {
console.log(msg);
});
});
//--------------------------------------------------------------------------------------
//redis.multi()
// .set('AAAA','AAAA')
// .set('BBBB','BBBB')
// .execAsync().then(function(res) {
// console.log(res);
// });
//-----------------------------------------------------------------------------------
var init_redis = function() {
return [
redis.delAsync(IP_AVATAR_PAIR_HSET_KEY),
redis.delAsync(AVATAR_SET_KEY),
readChatIDsFromMysql().then(function(ids_from_mysql) {
return Q.map(ids_from_mysql, function(id_from_mysql) {
return redis.zaddAsync(CHAT_IDS_ZSET_KEY, id_from_mysql, id_from_mysql);
});
}),
Q.map(AVATARS,function(val) {
return redis.saddAsync(AVATAR_SET_KEY, val);
}),
Q.map(redis.keysAsync(IP_AVATAR_PAIR_HISTORY_KEY_PREFIX+'*'),function(key) {
return redis.delAsync(key);
})
];
};
Q.all(init_redis()).then(function(arr) {
http.listen(4567, function() {
console.log('listening on *:4567');
});
});
//======================================================================================