-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigureSockets.js
103 lines (89 loc) · 3.18 KB
/
configureSockets.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
const jwt = require('jsonwebtoken');
const {
changeIsUserOnline,
addUserMessage,
addManagerMessage,
connectManager,
disconnectManager,
toggleChat,
leavePage,
} = require('./helpers/chatHelper');
module.exports = function configureSockets(socketIO, SECRET_KEY) {
const socketUserMap = new Map();
socketIO.on('connection', socket => {
// Processing of user or manager auth
socket.on('authentication', async ({ token }) => {
try {
const decoded = jwt.verify(token, SECRET_KEY);
const { userId, id } = decoded;
// Connecting a socket with userId
userId
? socketUserMap.set(socket.id, userId)
: socketUserMap.set(socket.id, id);
await changeIsUserOnline(socketIO, userId, true);
console.log(
`Socket ${socket.id} is authenticated for ${
userId ? 'user' : 'manager'
} ${userId || id}`,
);
} catch (err) {
socket.emit('authenticationError', {
message: 'Авторизація неуспішна. Повторно зайдіть в чат',
});
socket.disconnect();
}
});
// Processing when chat is minimized or extended
socket.on('toggleChat', async ({ userId, isChatRoomOpen }) => {
await toggleChat(socketIO, userId, isChatRoomOpen);
});
// Processing when chat is minimized or extended
socket.on('leavePage', async ({ userId, isLeavePage }) => {
await leavePage(socketIO, userId, isLeavePage);
});
// Processing of user message
socket.on('userMessage', async ({ userId, roomId, message }) => {
await addUserMessage(socketIO, userId, roomId, message);
});
// Processing of user typing
socket.on('userTyping', async ({ isTyping, roomId }) => {
await socketIO.emit('userTyping', { isTyping, roomId });
});
// Processing of manager connection
socket.on('managerJoinToChat', async ({ userId, roomId, manager }) => {
await connectManager(socketIO, userId, roomId, manager);
});
// Processing of manager message
socket.on('managerMessage', async ({ userId, roomId, message }) => {
await addManagerMessage(socketIO, userId, roomId, message);
});
// Processing of unread manager messages by User
socket.on(
'countUnreadManagerMessages',
async ({ userId, countUnreadManagerMessages }) => {
await socketIO.emit('countUnreadManagerMessages', {
userId,
countUnreadManagerMessages,
});
},
);
// Processing of manager typing
socket.on('managerTyping', async ({ isTyping, roomId }) => {
await socketIO.emit('managerTyping', { isTyping, roomId });
});
// Processing when user or manager disconnected
socket.on('disconnect', async () => {
const userId = socketUserMap.get(socket.id);
if (userId) {
await changeIsUserOnline(socketIO, userId, false);
await disconnectManager(socketIO, userId);
socketUserMap.delete(socket.id);
console.log(
`${socket.id} socket (user or manager Id: ${userId}) was disconnected`,
);
} else {
console.log('User or manager was disconnected');
}
});
});
};