-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.ts
More file actions
43 lines (35 loc) · 1.14 KB
/
Copy pathserver.ts
File metadata and controls
43 lines (35 loc) · 1.14 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
import { createServer } from "http";
import { parse } from "url";
import next from "next";
import { Server } from "socket.io";
import { GameManager } from "./src/server/game-manager";
import { autoStartTunnel } from "./src/lib/tunnel";
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = parseInt(process.env.PORT || "3000", 10);
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const httpServer = createServer((req, res) => {
const parsedUrl = parse(req.url!, true);
handle(req, res, parsedUrl);
});
const io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
transports: ["websocket", "polling"],
});
// Initialize game manager
const gameManager = new GameManager(io);
io.on("connection", (socket) => {
gameManager.handleConnection(socket);
});
httpServer.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
console.log(`> Socket.io server running`);
// Auto-start ngrok tunnel if token exists in database
autoStartTunnel();
});
});