-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (115 loc) · 3.58 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
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const path = require('path');
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/DATABASE_CHART";
const onlineUsers = [];
let usersNum = 0;
server.listen(3000, err => {
if(err) {
throw(err);
}
console.log("server running at 127.0.0.1:3000");
});
app.get('/', (req, res) => {
res.redirect('/static/chat.html');
})
app.use('/static', express.static(path.join(__dirname, './public')));
io.on('connection', (socket) => {
socket.on('login', (data) => {
checkUser(data, socket);
});
/**
* 监听sendMessage
*/
socket.on('sendMessage', (data) => {
for(let _user of onlineUsers) {
if(_user.username === data.username) {
_user.message.push(data.message);
//信息存储之后触发receiveMessage将信息发给所有浏览器-广播事件
io.emit('receiveMessage', data);
break;
}
}
});
socket.on('disconnect', () => {
usersNum = onlineUsers.length;
console.log(`当前在线登录人数:${usersNum}`);
});
})
const addOnlineUser = data => {
onlineUsers.push({
username: data.username,
message: []
});
usersNum = onlineUsers.length;
console.log(`用户${data.username}登录成功,进入聊天室,当前在线人数:${usersNum}`);
}
const connectDB = () => {
return new Promise((resolve, reject) => {
MongoClient.connect(url, function(err, db) {
if(err) return reject(err);
const dbo = db.db("DATABASE_CHART");
const collection = dbo.collection('userlist');
resolve({
db: db,
collection
});
});
});
}
const isRegister = (dbObj, name) => {
return new Promise((resolve, reject) => {
dbObj.collection.find({ username: name }).toArray(function(err, result) {
if(err) return reject(err);
resolve(Object.assign(dbObj, { result: result }))
});
});
}
const addUser = (dbObj, userData) => {
return new Promise((resolve, reject) => {
dbObj.collection.insertOne(userData, function(err, res) {
if(err) return reject(err);
resolve(Object.assign(dbObj, res));
dbObj.db.close();
});
});
}
const isLogin = (data) => {
let flag = false;
onlineUsers.map(user => {
if(user.username === data.username) {
flag = true;
}
});
return flag;
}
const checkUser = (data, socket) => {
connectDB().then(dbObj => {
return isRegister(dbObj, data.username);
}).then(dbObj => {
const userData = dbObj.result || [];
if(userData.length > 0) {
if(userData[0].password === data.password) {
if(isLogin(data)) {
socket.emit('loginResult', { code: 3 });
} else {
addOnlineUser(data);
socket.emit('loginResult', { code: 0 });
}
} else {
socket.emit('loginResult', { code: 1 });
}
dbObj.db.close();
} else {
addUser(dbObj, data).then(resolve => {
addOnlineUser(data);
socket.emit('loginResult', { code: '2-0' });
}, reject => {
socket.emit('loginResult', { code: '2-1' });
}).catch(err => console.log(err));
}
});
}