-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (89 loc) · 3.46 KB
/
index.js
File metadata and controls
113 lines (89 loc) · 3.46 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
const app = require("express")();
const server = require("http").createServer(app);
const cors = require("cors");
const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
app.use(cors());
const PORT = process.env.PORT || 5000;
app.get('/', (req, res) => {
res.send('Running');
});
// Global state for users
const users = {}; // { socketId: { id: socketId, status: 'idle' | 'waiting' | 'in-call' } }
io.on("connection", (socket) => {
socket.emit("me", socket.id);
// Register user
users[socket.id] = { id: socket.id, status: 'idle' };
io.emit("users_count", Object.keys(users).length);
socket.on("disconnect", () => {
socket.broadcast.emit("callEnded");
delete users[socket.id];
io.emit("users_count", Object.keys(users).length);
});
socket.on("callUser", ({ userToCall, signalData, from, name }) => {
io.to(userToCall).emit("callUser", { signal: signalData, from, name });
});
socket.on("answerCall", (data) => {
io.to(data.to).emit("callAccepted", data.signal)
});
// OLD Logic - kept for backward compatibility if needed
socket.on("find_match", () => {
console.log(`User ${socket.id} looking for match...`);
users[socket.id].status = 'waiting';
// Find a partner who is ALSO waiting and NOT self
const partnerId = Object.keys(users).find(id =>
id !== socket.id && users[id].status === 'waiting'
);
if (partnerId) {
console.log(`Match found: ${socket.id} <-> ${partnerId}`);
// Update statuses
users[socket.id].status = 'in-call';
users[partnerId].status = 'in-call';
// Notify both
// Initiator: The one who just triggered the match (socket.id)
io.to(socket.id).emit("match_found", { partnerId, initiator: true });
io.to(partnerId).emit("match_found", { partnerId: socket.id, initiator: false });
}
});
// NEW Logic - Scheduled Sessions
socket.on("join_session", ({ sessionId }) => {
const roomId = `session_${sessionId}`;
console.log(`User ${socket.id} joining ${roomId}`);
const room = io.sockets.adapter.rooms.get(roomId);
const clients = room ? room.size : 0;
if (clients >= 2) {
// Check if this socket is already in the room (re-join?)
if (room.has(socket.id)) {
return;
}
socket.emit("session_full");
return;
}
socket.join(roomId);
users[socket.id].status = 'waiting'; // Or 'in-session-waiting'
if (clients === 1) {
// There was 1 person, now 2. Match them.
// Get the other socket ID.
// Note: socket.join is synchronous, so 'room' check above needs refresh or logic adjustment.
// Actually 'room' variable is a reference or copy? get() returns a generic Set, likely live?
// No, get() returns a Set at that moment.
// Let's just re-fetch or use the known state.
const updatedRoom = io.sockets.adapter.rooms.get(roomId);
// updatedRoom has both IDs.
const otherSocketId = [...updatedRoom].find(id => id !== socket.id);
if (otherSocketId) {
console.log(`Session Match: ${socket.id} <-> ${otherSocketId}`);
users[socket.id].status = 'in-call';
users[otherSocketId].status = 'in-call';
// Trigger the same flow as random match
io.to(socket.id).emit("match_found", { partnerId: otherSocketId, initiator: true });
io.to(otherSocketId).emit("match_found", { partnerId: socket.id, initiator: false });
}
}
});
});
server.listen(PORT, () => console.log(`Server is running on port ${PORT}`));