-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketServer.js
106 lines (87 loc) · 2.84 KB
/
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
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
let users = [];
const EditData = (data, id, call) => {
const newData = data.map((item) =>
item.id === id ? { ...item, call } : item
);
return newData;
};
const SocketServer = (socket) => {
// Connect - Disconnect
socket.on('joinUser', (user) => {
users.push({
id: user._id,
socketId: socket.id,
});
});
socket.on('disconnect', () => {
const data = users.find((user) => user.socketId === socket.id);
if (data) {
if (data.call) {
const callUser = users.find((user) => user.id === data.call);
if (callUser) {
users = EditData(users, callUser.id, null);
socket.to(`${callUser.socketId}`).emit('callerDisconnect');
}
}
}
users = users.filter((user) => user.socketId !== socket.id);
});
// Notification
socket.on('createNotify', (msgs) => {
const client = users.find((user) => msgs.recipients.includes(user.id));
client &&
socket.to(`${client.socketId}`).emit('createNotifyToClient', msgs);
});
socket.on('removeNotify', (msg) => {
const client = users.find((user) => msg.recipients.includes(user.id));
client && socket.to(`${client.socketId}`).emit('removeNotifyToClient', msg);
});
// Message
socket.on('addMessage', (msg) => {
const user = users.find((user) => user.id === msg.recipient);
user && socket.to(`${user.socketId}`).emit('addMessageToClient', msg);
});
// Check User Online / Offline
socket.on('checkUserOnline', (data) => {
const following = users.filter((user) => {
return user.id;
});
socket.emit('checkUserOnlineToMe', following);
const clients = users.filter((user) => user.id);
if (clients.length > 0) {
clients.forEach((client) => {
socket
.to(`${client.socketId}`)
.emit('checkUserOnlineToClient', data._id);
});
}
});
// Call User
socket.on('callUser', (data) => {
users = EditData(users, data.sender, data.recipient);
const client = users.find((user) => user.id === data.recipient);
if (client) {
if (client.call) {
socket.emit('userBusy', data);
users = EditData(users, data.sender, null);
} else {
users = EditData(users, data.recipient, data.sender);
socket.to(`${client.socketId}`).emit('callUserToClient', data);
}
}
});
socket.on('endCall', (data) => {
const client = users.find((user) => user.id === data.sender);
if (client) {
socket.to(`${client.socketId}`).emit('endCallToClient', data);
users = EditData(users, client.id, null);
if (client.call) {
const clientCall = users.find((user) => user.id === client.call);
clientCall &&
socket.to(`${clientCall.socketId}`).emit('endCallToClient', data);
users = EditData(users, client.call, null);
}
}
});
};
module.exports = SocketServer;