-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (28 loc) · 866 Bytes
/
Copy pathserver.js
File metadata and controls
35 lines (28 loc) · 866 Bytes
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
// Agar.io Clone Server
const path = require("path");
const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const PORT = process.env.PORT || 3000;
const app = express();
app.use(express.json());
// Serve static frontend in production from the public folder
app.use(express.static(path.join(__dirname, "public")));
app.get(/.*/, (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
const server = http.createServer(app);
const io = new Server(server, {
cors: { origin: "*" },
});
// WebSocket handlers
io.on("connection", (socket) => {
const id = socket.id;
socket.on("disconnect", () => {
console.log(`Player disconnected: ${id}`);
});
});
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
module.exports = { io, app };