-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketServer.js
167 lines (145 loc) · 5.49 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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, followers: user.followers });
})
socket.on('disconnect', () => {
const data = users.find(user => user.socketId === socket.id)
if (data) {
const clients = users.filter(user =>
data.followers.find(item => item._id === user.id)
)
if (clients.length > 0) {
clients.forEach(client => {
socket.to(`${client.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)
})
// Likes
socket.on('likePost', newPost => {
const ids = [...newPost.user.followers, newPost.user._id]
const clients = users.filter(user => ids.includes(user.id))
if (clients.length > 0) {
clients.forEach(client => {
socket.to(`${client.socketId}`).emit('likeToClient', newPost)
})
}
})
socket.on('unLikePost', newPost => {
const ids = [...newPost.user.followers, newPost.user._id]
const clients = users.filter(user => ids.includes(user.id))
if (clients.length > 0) {
clients.forEach(client => {
socket.to(`${client.socketId}`).emit('unLikeToClient', newPost)
})
}
})
// Comments
socket.on('createComment', newPost => {
const ids = [...newPost.user.followers, newPost.user._id]
const clients = users.filter(user => ids.includes(user.id))
if (clients.length > 0) {
clients.forEach(client => {
socket.to(`${client.socketId}`).emit('createCommentToClient', newPost)
})
}
})
socket.on('deleteComment', newPost => {
const ids = [...newPost.user.followers, newPost.user._id]
const clients = users.filter(user => ids.includes(user.id))
if (clients.length > 0) {
clients.forEach(client => {
socket.to(`${client.socketId}`).emit('deleteCommentToClient', newPost)
})
}
})
// Follow
socket.on('follow', newUser => {
const user = users.find(user => user.id === newUser._id)
user && socket.to(`${user.socketId}`).emit('followToClient', newUser)
})
socket.on('unFollow', newUser => {
const user = users.find(user => user.id === newUser._id)
user && socket.to(`${user.socketId}`).emit('unFollowToClient', newUser)
})
// Notification
socket.on('createNotify', msg => {
const clients = users.filter(user => msg.recipients.includes(user.id))
if (clients.length > 0) {
clients.forEach(client => {
socket.to(`${client.socketId}`).emit('createNotifyToClient', msg)
})
}
})
socket.on('removeNotify', msg => {
const clients = users.filter(user => msg.recipients.includes(user.id))
if (clients.length > 0) {
clients.forEach(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 => data.following.find(item => item._id === user.id)
)
socket.emit('checkUserOnlineToMe', following)
const clients = users.filter(user =>
data.followers.find(item => item._id === 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){
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;