-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
62 lines (51 loc) · 1.88 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
import {
handleMessage,
fetchVideoChatData,
loadMessagesHandler,
handleLookMessage,
} from "./controllers/socket/messageController.js";
import {
addDMHandler,
getDMsHandler,
} from "./controllers/socket/userController.js";
import User from "./models/userModels.js";
global.onlineUsers = new Map();
global.onlineSupports = new Map();
const socketConnectionManager = function (socket, io) {
socket.on("add-user", async (userId) => {
console.log("add-user");
const userData = await User.findById(userId);
// for (var entry of onlineSupports.entries()) {
// var key = entry[0], value = entry[1];
// console.log(key, value);
// }
let userSocket = onlineSupports.get(userId);
if(userSocket) {
console.log("logout automatically");
socket.to(userSocket).emit("logout", {});
}
if(userData){
if(userData.role != "0" && userData.role != 0 ) {
onlineSupports.set(userId, socket.id);
// console.log("added on support");
// console.log("---- current supports ----");
// for (var entry of onlineSupports.entries()) {
// var key = entry[0], value = entry[1];
// console.log(key, value);
// }
// console.log("----------------");
} else {
onlineUsers.set(userId, socket.id);
console.log("added on normal user");
}
}
});
socket.on("message:send", handleMessage);
socket.on("message:looked", handleLookMessage);
socket.on("messages:read", loadMessagesHandler);
socket.on("DMs:create", addDMHandler);
socket.on('disconnect', () => {
console.log("user disconnected ");
});
};
export default socketConnectionManager;