forked from advanced-computer-lab-2023/CodeMedics-Clinic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketHandler.js
More file actions
83 lines (68 loc) · 2.41 KB
/
socketHandler.js
File metadata and controls
83 lines (68 loc) · 2.41 KB
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
const { Server } = require("socket.io");
const { putSocket, getSocket } = require("./config/socket");
const rooms = new Map();
const initializeSocket = (server) => {
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
socket.on("iAmReady", (username) => {
console.log("iAmReady: " + username, socket.id);
io.to(socket.id).emit("me", socket.id);
putSocket(username, socket.id);
});
socket.on("create-room", (roomId) => {
if (!rooms.has(roomId)) {
rooms.set(roomId, { participants: [] });
console.log("Room created:", roomId);
}
});
socket.on("join-room", (roomId, userId, name) => {
if (!rooms.has(roomId)) {
socket.emit("invalid-room");
return;
}
const room = rooms.get(roomId);
if (!room.participants.some((p) => p.userId === userId)) {
room.participants.push({ userId, name });
socket.join(roomId);
socket.to(roomId).emit("user-connected", { userId, name });
socket.emit(
"existing-users",
room.participants.filter((p) => p.userId !== userId)
);
}
socket.on("disconnect", () => {
room.participants = room.participants.filter((p) => p.userId !== userId);
console.log("User disconnected:", userId);
socket.to(roomId).emit("user-disconnected", userId);
});
});
socket.on("offer", (userId, offer) => {
socket.broadcast.to(userId).emit("offer", socket.id, offer);
});
socket.on("answer", (userId, answer) => {
socket.broadcast.to(userId).emit("answer", socket.id, answer);
});
socket.on("ice-candidate", (userId, candidate) => {
socket.broadcast.to(userId).emit("ice-candidate", socket.id, candidate);
});
socket.on("newMessage", async ({ message, receiver }) => {
const socketId = await getSocket(receiver);
if (socketId) io.to(socketId).emit("newMessage", message);
});
socket.on("newMessagePharmacy", async ({ message, receiver, sendingToPharmacy }) => {
const socketId = await getSocket(receiver);
const room = receiver + " room";
if (sendingToPharmacy) {
io.to(room).emit("newMessagePharmacy", message);
} else {
io.to(socketId).to(room).emit("newMessagePharmacy", message);
}
});
});
};
module.exports = { initializeSocket };