Repository navigation
Expand file tree
/
Copy pathApp.ts
More file actions
108 lines (89 loc) · 3.09 KB
/
Copy pathApp.ts
File metadata and controls
108 lines (89 loc) · 3.09 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
import express, { Application } from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import http from 'http';
import morgan from "morgan";
import connectDB from './Config/db';
import cookieParser from "cookie-parser";
import passport from "passport";
import { passportInit } from './Controllers/Authentication/auth';
import { routes } from './Routes';
import { Server } from 'socket.io';
import path from 'path';
import MatchMaking from './Models/Challenges/MatchMaking';
import { MatchmakingService } from './Services/ChallengeServices/matchmakingService';
import { initSocket, io, userSockets } from './Config/socket';
import './Workers/Challenges/CodeWorker';
dotenv.config();
const app = express();
const server = http.createServer(app);
initSocket(server);
app.use(
cors({
credentials: true,
origin: [
process.env.REACT_APP_BASE_URL,
process.env.REACT_APP_BASE_URL2,
process.env.REACT_APP_BASE_URL3,
'https://www.code-space.tech',
'https://code-space.tech',
],
}),
);
// Middleware
app.use(express.json());
// Database connection
connectDB();
app.use(cookieParser());
app.use(morgan("dev"));
app.use(passport.initialize());
passportInit(passport);
routes(app);
io.on("connection", (socket) => {
console.log(`🔌 New connection: ${socket.id}`);
socket.on("register", (userId) => {
if (!userSockets.has(userId)) {
userSockets.set(userId, new Set());
}
userSockets.get(userId).add(socket.id);
});
socket.on("join_queue", async ({ userId, language, timeControl, timezone }) => {
try {
console.log(`🎯 ${userId} joined matchmaking queue`);
const result = await MatchmakingService.joinMatchmaking(
userId,
language,
timeControl,
timezone
);
if (result.matchFound) {
console.log(`✅ Match found for ${userId}`);
// MatchmakingService handles socket emits internally now
} else {
socket.emit("queued", { message: "Searching for a match..." });
}
} catch (err) {
console.error("Error in join_queue:", err);
socket.emit("error_message", { message: "Internal server error" });
}
});
socket.on("disconnect", () => {
userSockets.forEach((socketIds, userId) => {
if (socketIds.has(socket.id)) {
socketIds.delete(socket.id);
console.log(`❌ User ${userId} disconnected from socket ${socket.id}`);
if (socketIds.size === 0) {
userSockets.delete(userId);
console.log(`💨 All sockets for user ${userId} are disconnected.`);
// Cleanup matchmaking entry
MatchMaking.deleteOne({ user_id: userId }).then(() => {
console.log(`🧹 Removed user ${userId} from matchmaking queue.`);
}).catch(err => console.error("Error removing user from matchmaking queue:", err));
}
}
});
});
});
// Start server
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));