-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
317 lines (291 loc) · 8.8 KB
/
server.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const express = require("express");
const http = require("http");
const WebSocket = require("ws");
const path = require("path");
const db = require("./db");
const roomService = require("./services/roomService");
const voteService = require("./services/voteService");
const jwt = require("jsonwebtoken");
const SECRET_KEY = process.env.JWT_SECRET || "your-secret-key";
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(express.static("public"));
app.use(express.json());
// Middleware to verify JWT token
const verifyToken = (req, res, next) => {
const token = req.headers["authorization"];
if (!token) return res.status(403).json({ error: "No token provided" });
jwt.verify(token, SECRET_KEY, (err, decoded) => {
if (err)
return res.status(500).json({ error: "Failed to authenticate token" });
req.clientId = decoded.clientId;
next();
});
};
// Routes
app.get("/", (req, res) =>
res.sendFile(path.join(__dirname, "public", "index.html"))
);
app.post("/create-room", async (req, res) => {
try {
const { clientId } = req.body;
if (!clientId) {
return res.status(400).json({ error: "Client ID is required" });
}
const roomId = await roomService.createRoom(clientId);
const token = jwt.sign({ clientId }, SECRET_KEY, { expiresIn: "1h" });
res.json({ roomId, token });
} catch (error) {
console.error("Error creating room:", error);
res.status(500).json({ error: "Failed to create room" });
}
});
app.get("/room/:roomId", async (req, res) => {
try {
const room = await roomService.getRoomData(req.params.roomId);
if (room) {
res.sendFile(path.join(__dirname, "public", "room.html"));
} else {
res.redirect("/");
}
} catch (error) {
console.error("Error checking room existence:", error);
res.status(500).send("Internal Server Error");
}
});
// Modify the route to check if a user is the host
app.get("/api/is-host/:roomId/:clientId", async (req, res) => {
try {
const isHost = await roomService.isRoomHost(
req.params.roomId,
req.params.clientId
);
res.json({ isHost });
} catch (error) {
console.error("Error checking host status:", error);
res.status(500).json({ error: "Failed to check host status" });
}
});
// WebSocket handling
wss.on("connection", (ws, req) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const token = url.searchParams.get("token");
if (token) {
jwt.verify(token, SECRET_KEY, (err, decoded) => {
if (err) {
console.error("Invalid token:", err);
ws.close(1008, "Invalid token");
return;
}
ws.clientId = decoded.clientId;
setupWebSocketHandlers(ws);
});
} else {
// Allow connection without token, but don't set clientId
setupWebSocketHandlers(ws);
}
});
function setupWebSocketHandlers(ws) {
ws.on("message", async (message) => {
const data = JSON.parse(message);
switch (data.type) {
case "join":
await handleJoin(ws, data);
break;
case "vote":
await handleVote(ws, data);
break;
case "start-vote":
await handleStartVote(ws, data);
break;
case "end-vote":
await handleEndVote(ws);
break;
case "update-options":
await handleUpdateOptions(ws, data);
break;
case "request-new-vote":
await handleRequestNewVote(ws);
break;
}
});
}
async function handleJoin(ws, data) {
try {
const room = await roomService.getRoomData(data.roomId);
if (room) {
ws.roomId = data.roomId;
ws.clientId = data.clientId;
console.log(`Client joined: ${ws.clientId}`); // Add this line for debugging
ws.send(
JSON.stringify({
type: "joined",
roomId: data.roomId,
isHost: room.host_id === data.clientId,
status: room.voting_active ? "active" : "waiting",
options: room.options,
})
);
if (room.voting_active) {
const votes = await voteService.getVotes(data.roomId);
ws.send(
JSON.stringify({ type: "results", results: calculateResults(votes) })
);
}
} else {
ws.send(JSON.stringify({ type: "error", message: "Room not found" }));
}
} catch (error) {
console.error("Error joining room:", error);
ws.send(JSON.stringify({ type: "error", message: "Error joining room" }));
}
}
async function handleVote(ws, data) {
try {
const room = await roomService.getRoomData(ws.roomId);
if (room && room.voting_active && new Date() < room.voting_end_time) {
if (!ws.clientId) {
throw new Error("Client ID is not set");
}
await voteService.addVote(ws.roomId, ws.clientId, data.value.toString());
ws.send(
JSON.stringify({
type: "voteConfirmation",
value: data.value,
})
);
const votes = await voteService.getVotes(ws.roomId);
broadcastToRoom(ws.roomId, {
type: "results",
results: calculateResults(votes),
});
} else {
ws.send(
JSON.stringify({
type: "error",
message: "Voting is not active or has ended",
})
);
}
} catch (error) {
console.error("Error processing vote:", error);
ws.send(
JSON.stringify({
type: "error",
message: error.message,
})
);
}
}
async function handleStartVote(ws, data) {
try {
const room = await roomService.getRoomData(ws.roomId);
if (room && room.host_id === ws.clientId && !room.voting_active) {
const votingDuration = data.duration;
const votingEndTime = new Date(Date.now() + votingDuration);
await roomService.updateRoomVotingStatus(
ws.roomId,
true,
votingDuration,
votingEndTime
);
broadcastToRoom(ws.roomId, { type: "status", status: "active" });
setTimeout(() => {
endVoting(ws.roomId);
}, votingDuration);
}
} catch (error) {
console.error("Error starting vote:", error);
ws.send(JSON.stringify({ type: "error", message: "Error starting vote" }));
}
}
async function handleEndVote(ws) {
try {
const room = await roomService.getRoomData(ws.roomId);
if (room && room.host_id === ws.clientId && room.voting_active) {
await endVoting(ws.roomId);
}
} catch (error) {
console.error("Error ending vote:", error);
ws.send(JSON.stringify({ type: "error", message: "Error ending vote" }));
}
}
async function endVoting(roomId) {
await roomService.updateRoomVotingStatus(roomId, false, 0, null);
broadcastToRoom(roomId, { type: "status", status: "ended" });
const votes = await voteService.getVotes(roomId);
broadcastToRoom(roomId, {
type: "results",
results: calculateResults(votes),
});
}
function calculateResults(votes) {
const voteCounts = {};
const totalVotes = votes.length;
for (const vote of votes) {
voteCounts[vote.vote] = (voteCounts[vote.vote] || 0) + 1;
}
return Object.entries(voteCounts)
.map(([rating, count]) => ({
rating: rating,
count,
percentage: totalVotes > 0 ? Math.round((count / totalVotes) * 100) : 0,
}))
.sort((a, b) => b.rating - a.rating);
}
function broadcastToRoom(roomId, message) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.roomId === roomId) {
client.send(JSON.stringify(message));
}
});
}
async function handleUpdateOptions(ws, data) {
try {
const room = await roomService.getRoomData(data.roomId);
if (room && room.host_id === ws.clientId && !room.voting_active) {
await roomService.updateRoomOptions(data.roomId, data.options);
broadcastToRoom(data.roomId, {
type: "options-updated",
options: data.options,
});
} else {
ws.send(
JSON.stringify({
type: "error",
message: "Unauthorized or voting is active",
})
);
}
} catch (error) {
console.error("Error updating options:", error);
ws.send(
JSON.stringify({ type: "error", message: "Error updating options" })
);
}
}
async function handleRequestNewVote(ws) {
try {
const room = await roomService.getRoomData(ws.roomId);
if (room && room.host_id === ws.clientId && !room.voting_active) {
broadcastToRoom(ws.roomId, { type: "new-vote-requested" });
} else {
ws.send(
JSON.stringify({
type: "error",
message: "Unauthorized or voting is active",
})
);
}
} catch (error) {
console.error("Error requesting new vote:", error);
ws.send(
JSON.stringify({ type: "error", message: "Error requesting new vote" })
);
}
}
db.sequelize.sync().then(() => {
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
});