-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket.js
127 lines (91 loc) · 4.59 KB
/
socket.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 socket = require('socket.io');
const { User } = require('./models/User');
const { Room } = require('./models/Room');
const socketMap = {};
let inVideoCallUser = [];
module.exports = (server) => {
const io = socket(server);
io.on('connection', socket => {
console.log('A new client connected to server', socket.id);
socket.on('thisUserCreatesRoom', data => {
const { users, roomInfo } = data;
users.forEach(user => socketMap[user] && socketMap[user].join(roomInfo.roomId));
socket.broadcast.to(roomInfo.roomId).emit('aUserCreatesRoom', data);
});
socket.on('thisUserGoesOnline', data => {
const { username } = data;
const now = new Date();
console.log(username, 'goes online');
socket.broadcast.emit('aUserGoesOnline', { ...data, lastLogin: now.toISOString() });
socketMap[username] = socket;
User.findOneAndUpdate({ username }, { $set: { lastLogin: now.toISOString() } }, function () { });
User.findOne({ username }).then(user => {
const { rooms } = user;
rooms.forEach(room => socket.join(room));
});
});
socket.on('thisUserGoesOffline', data => {
const { username } = data;
const now = new Date();
console.log(data.username, 'goes offline');
socket.broadcast.emit('aUserGoesOffline', { ...data, lastLogout: now.toISOString() });
delete socketMap[username];
User.findOneAndUpdate({ username }, { $set: { lastLogout: now.toISOString() } }, function () { });
User.findOne({ username }).then(user => {
const { rooms } = user;
console.log('error');
rooms.forEach(room => socket.leave(room._id));
});
inVideoCallUser = inVideoCallUser.filter(user => user !== username);
});
socket.on('thisUserMakesVideoCall', (data, cb) => {
console.log('A user makes video call.', data.counterpart, data.from, inVideoCallUser);
if (inVideoCallUser.indexOf(data.from) !== -1) return cb('YOU_ARE_CALLING');
if (inVideoCallUser.indexOf(data.counterpart) !== -1) return cb('USER_IS_CALLING');
inVideoCallUser.push(data.from);
cb('OK');
socket.broadcast.to(data.roomId).emit('aUserMakesVideoCall', data);
});
socket.on('thisUserJoinsVideoCall', data => {
inVideoCallUser.push(data.from);
});
socket.on('thisUserQuitsVideoCall', data => {
inVideoCallUser = inVideoCallUser.filter(user => user !== data.from);
socket.broadcast.emit('aUserQuitsVideoCall', data);
});
socket.on('thisUserDeclinesVideoCall', data => {
socket.broadcast.emit('aUserDeclinesVideoCall', data);
});
socket.on('thisUserSendsMessage', data => {
const now = new Date();
socket.broadcast.to(data.roomId).emit('aUserSendsMessage', { ...data, time: now.toISOString() });
});
socket.on('thisUserIsTyping', data => {
socket.broadcast.to(data.roomId).emit('aUserIsTyping', data);
});
socket.on('thisUserStopsTyping', data => {
socket.broadcast.to(data.roomId).emit('aUserStopsTyping', data);
});
socket.on('thisUserChangesColorTheme', data => {
const { roomId, content, type } = data;
const now = new Date();
socket.broadcast.to(data.roomId).emit('aUserChangesColorTheme', { ...data, time: now.toISOString() });
socket.emit('aUserChangesColorTheme', { ...data, time: now.toISOString() });
Room.findByIdAndUpdate(roomId, { $push: { messages: { $each: [{ from: 'system', content, type }] } } }, function () { });
});
socket.on('thisUserSeesMessage', async data => {
const now = new Date();
socket.broadcast.to(data.roomId).emit('aUserSeesMessage', { ...data, time: now.toISOString() });
const { roomId, from } = data;
let room = await Room.findById(roomId);
let messages = [...room.messages];
for (let i = messages.length - 1; i >= 0; i--) {
if (messages[i].peopleSeen.map(user => user.username).indexOf(from) === -1 && messages[i].from !== from && messages[i].from !== 'system') {
messages[i].peopleSeen.push({ username: from });
}
}
room.set({ messages });
room.save();
});
});
}