This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (126 loc) · 5.43 KB
/
server.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
//Create by Geoffrey Cheung 2015
//Server libraries
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongo = require('mongodb').MongoClient;
var cookieParser = require('cookie-parser');
var cookie = require('cookie');
var fs = require('fs');
//Utility libraries
var Hashids = require("hashids");
var aes = require("./util/aes.js");
//Config file
var server_config = require('./config/server_config.js');
var basic_config = JSON.parse(fs.readFileSync('./config/basic_config.json', 'utf8'));
var security_config = JSON.parse(fs.readFileSync('./config/security_config.json', 'utf8'));
var user_count = 0;
var max_message_num = basic_config.max_message_num;
var id_ttl_day = basic_config.id_ttl_day;
var msg_ttl_day = basic_config.msg_ttl_day;
var d = new Date();
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/js', express.static(__dirname + '/app/js'));
app.use('/css', express.static(__dirname + '/app/css'));
app.use(cookieParser());
app.get('/', function (req, res) {
res.sendFile(__dirname + '/app/index.html');
var cookie = req.cookies.userID;
if (cookie === undefined)
{
var conn_d = new Date();
hashids = new Hashids(conn_d.toString());
var id = hashids.encode(Math.floor(Math.random() * (10)) + 1,Math.floor(Math.random() * (10)) + 1,Math.floor(Math.random() * (10)) + 1,Math.floor(Math.random() * (10)) + 1);
res.cookie('userID',id,{maxAge:3600000 * 24 * id_ttl_day});
}
});
//When client connected
io.on('connection', function (socket) {
var clientIp = socket.request.connection.remoteAddress;
var id = cookie.parse(socket.request.headers.cookie).userID;
var mongodb_uri = server_config.getMongodbURI ();
console.log('User from:' + clientIp + ' connected.');
user_count+=1;
io.emit('get online user',user_count);
console.log('Online: '+ user_count);
//When DB connected
mongo.connect(mongodb_uri, function (err, db) {
if (err != null) {
console.log(err.name + ": " + err.message);
}else{
if (db.collection('chatroom').find() === undefined)
{
console.log('Cannot find the collection, now we will create one.');
db.createCollection('chatroom');
db.collection.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 * 24 * msg_ttl_day} );
}
var collection = db.collection('chatroom');
collection.count(function (err, count) {
if(count > max_message_num)
{
var stream = collection.find().sort({"createdAt": 1}).skip(count - max_message_num).stream();
stream.on('data', function (data) {
io.to(socket.id).emit('chat message',
data.timestamp,
data.nickname,
data.encrypted ? aes.decrypt(data.message):data.message,
data.client_ID,
data.color);
});
}else{
var stream = collection.find().sort({"createdAt": 1}).stream();
stream.on('data', function (data) {
io.to(socket.id).emit('chat message',
data.timestamp,
data.nickname,
data.encrypted ? aes.decrypt(data.message):data.message,
data.client_ID,
data.color);
});
}
});
}
});
//When client disconnected
socket.on('disconnect', function () {
console.log('User from:' + clientIp + ' disconnected');
user_count-=1;
if(user_count > 0)
{
io.emit('get online user',user_count);
console.log('Online: '+ user_count);
}
});
//When messages recivied
socket.on('chat message', function (name, msg, color) {
var msgd = new Date();
var n = msgd.getTime();
mongo.connect(mongodb_uri, function (err, db) {
if (err) {
console.warn(err.name + ": " + err.message);
}else{
var collection = db.collection('chatroom');
collection.insert({
"createdAt": new Date(),
"timestamp": n,
"nickname": name,
"message": security_config.using_aes_to_store? aes.encrypt(msg) :msg,
"client_ID": id,
"color": color,
"encrypted": security_config.using_aes_to_store? 1:0,
}, function (err, o) {
if (err) {
console.warn(err.name + ": " + err.message);
} else {
console.log(d.toString() + '\nChat message form [' + clientIp + '] name [' + name + '] say [' + msg + ']');
}
});
}
});
io.emit('chat message', n, name, msg, id, color);
});
});
http.listen(server_config.port, server_config.ip, function () {
console.log('listening on ' + server_config.ip + ':' + server_config.port);
});