-
Notifications
You must be signed in to change notification settings - Fork 3
/
SocketServer.js
95 lines (78 loc) · 2.75 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
let users = []
const EditData = (data, id, call) => {
const newData = data.map(item => item.id === id ? { ...item, call } : item)
return newData
}
const socketServer = socket => {
socket.on('joinUser', user => {
users.push({id: user._id, socketId: socket.id})
})
socket.on('disconnect', () => {
const data = users.find(item => item.socketId === socket.id)
users.forEach(user => {
socket.to(`${user.socketId}`).emit('checkUserOffline', data?.id)
})
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)
})
socket.on('createMessage', data => {
const client = users.find(user => user.id === data.recipient._id)
if (client)
socket.to(`${client.socketId}`).emit('createMessageToClient', data)
})
socket.on('typing', data => {
const client = users.find(user => user.id === data.recipient)
if (client)
socket.to(`${client.socketId}`).emit('typingToClient', {
message: data.sender.name + ' is typing ...'
})
})
socket.on('doneTyping', data => {
const client = users.find(user => user.id === data.recipient)
if (client)
socket.to(`${client.socketId}`).emit('doneTypingToClient', data)
})
socket.on('checkUserOnline', data => {
users.forEach(user => {
socket.to(`${user.socketId}`).emit('checkUserOnlineToClient', users)
})
socket.emit('checkUserOnlineToClient', users)
})
socket.on('readMessage', data => {
const client = users.find(user => user.id === data.recipient)
if (client)
socket.to(`${client.socketId}`).emit('readMessageToClient', data)
})
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) {
users = EditData(users, data.sender, null)
socket.emit('userBusy', data)
} 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