-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (122 loc) · 4.61 KB
/
server.js
File metadata and controls
147 lines (122 loc) · 4.61 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
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
import { createServer } from "node:http";
import next from "next";
import { Server } from "socket.io";
const dev = process.env.NODE_ENV !== "production";
const PORT = parseInt(process.env.PORT || "3000", 10);
const HOST = process.env.HOST || "0.0.0.0";
const app = next({ dev });
const handler = app.getRequestHandler();
const roomQueues = new Map();
function getRoomQueue(roomId) {
if (!roomQueues.has(roomId)) {
roomQueues.set(roomId, new Map());
}
return roomQueues.get(roomId);
}
app.prepare().then(() => {
const httpServer = createServer((req, res) => handler(req, res));
const corsOrigins = process.env.CORS_ORIGINS
? process.env.CORS_ORIGINS.split(",").map((s) => s.trim())
: "*";
const io = new Server(httpServer, {
cors: { origin: corsOrigins, methods: ["GET", "POST"], credentials: true },
path: "/socket.io",
});
let onlineUsers = [];
io.on("connection", (socket) => {
console.log("New socket connected:", socket.id);
socket.on("addNewUser", (user) => {
const newUser = {
socketId: socket.id,
userId: user.id,
userName: user.name,
userEmail: user.email,
userRole: user.role,
};
onlineUsers = onlineUsers.filter((u) => u.userId !== user.id);
onlineUsers.push(newUser);
io.emit("getUsers", onlineUsers);
});
socket.on("disconnect", () => {
onlineUsers = onlineUsers.filter((u) => u.socketId !== socket.id);
io.emit("getUsers", onlineUsers);
});
socket.on("call", (participants) => {
const receiverSocketId = participants.receiver.socketId;
io.to(receiverSocketId).emit("inComingCall", participants);
});
socket.on("webrtcSignal", ({ sdp, ongoingCall, isCaller }) => {
const targetSocketId = isCaller
? ongoingCall.participants.receiver.socketId
: ongoingCall.participants.caller.socketId;
io.to(targetSocketId).emit("webrtcSignal", sdp);
});
socket.on("toggleVideo", ({ userId, isVidOn }) => {
const participant = onlineUsers.find((u) => u.userId !== userId);
if (participant)
io.to(participant.socketId).emit("remoteVideoToggle", isVidOn);
});
socket.on("sendMessage", (message) => {
const receiver = onlineUsers.find((u) => u.userId === message.receiverId);
if (receiver) io.to(receiver.socketId).emit("receiveMessage", message);
});
socket.on("doctorJoinRoom", ({ roomId, doctor }) => {
socket.join(roomId);
const queue = getRoomQueue(roomId);
for (const [, patient] of queue.entries()) {
io.to(socket.id).emit("patientJoinRequest", { roomId, patient });
}
io.to(roomId).emit("doctorPresent");
});
socket.on("patientJoinRequest", ({ roomId, patient }) => {
socket.join(roomId);
const fromOnline =
onlineUsers.find(
(u) => u.userId === (patient?.id || patient?.userId)
) || null;
const normalizedPatient = fromOnline
? fromOnline
: {
socketId: socket.id,
userId: String(patient?.id || patient?.userId || ""),
userName: String(patient?.name || patient?.userName || ""),
userEmail: String(patient?.email || patient?.userEmail || ""),
userRole: String(patient?.role || patient?.userRole || "patient"),
};
const queue = getRoomQueue(roomId);
if (normalizedPatient.userId) {
queue.set(normalizedPatient.userId, normalizedPatient);
}
const doctor = onlineUsers.find((u) => u.userRole === "doctor");
if (doctor) {
io.to(doctor.socketId).emit("patientJoinRequest", {
roomId,
patient: normalizedPatient,
});
}
});
socket.on("approvePatient", ({ roomId, patientId }) => {
const patient = onlineUsers.find((u) => u.userId === patientId);
const doctor = onlineUsers.find((u) => u.userRole === "doctor");
const queue = getRoomQueue(roomId);
queue.delete(patientId);
if (patient && doctor) {
const ongoingCall = {
participants: { caller: patient, receiver: doctor },
isRinging: false,
};
io.to(patient.socketId).emit("callApproved", ongoingCall);
io.to(doctor.socketId).emit("callApproved", ongoingCall);
}
});
socket.on("rejectPatient", ({ roomId, patientId }) => {
const queue = getRoomQueue(roomId);
queue.delete(patientId);
const patient = onlineUsers.find((u) => u.userId === patientId);
if (patient) io.to(patient.socketId).emit("joinRejected");
});
});
httpServer.listen(PORT, HOST, () => {
console.log(`> Ready on http://${HOST}:${PORT}`);
});
});