forked from Creatoon/Jar-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketServer.js
31 lines (29 loc) · 831 Bytes
/
socketServer.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
const MessageDB = require('./models/messageModel');
const User = require('./models/userModel');
const sock = socket => {
socket.on('join room', async data => {
socket.join(data.roomName);
const userName = await User.findById(data.userId);
socket.to(data.roomName).broadcast.emit('broadcast', {
message: data.message,
name: userName
});
const date = new Date();
let minute = date.getMinutes();
if (minute <= 9) {
minute = '0' + minute;
}
const newTime = `${date.getHours()}:${minute}`;
if (data.roomName !== 'default') {
const chat = new MessageDB({
message: data.message,
room: data.roomName,
user: data.userId,
userName: userName.name,
timestamp: newTime
});
chat.save();
}
});
};
module.exports = sock;