-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (40 loc) · 1.12 KB
/
Copy pathserver.js
File metadata and controls
50 lines (40 loc) · 1.12 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
import { createServer } from "http";
import { parse } from "url";
import next from "next";
import { Server } from "socket.io";
const dev = process.env.NODE_ENV !== "production";
const hostname = "0.0.0.0";
const port = process.env.PORT || 3000;
const app = next({ dev });
const handle = app.getRequestHandler();
await app.prepare();
const server = createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true);
await handle(req, res, parsedUrl);
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("Internal Server Error");
}
});
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
global.io = io;
io.on("connection", (socket) => {
console.log("Socket connected:", socket.id);
socket.on("join-room", (pollId) => {
socket.join(pollId);
console.log(`Socket ${socket.id} joined room ${pollId}`);
});
socket.on("disconnect", () => {
console.log("Socket disconnected:", socket.id);
});
});
server.listen(port, hostname, () => {
console.log(`> Server ready on port ${port}`);
});