-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
77 lines (60 loc) · 1.94 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
import { createServer } from "node:http";
import next from "next";
import { Server } from "socket.io";
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = 3000;
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
const games = {}; // { gameId: { players: [socketId, socketId] } }
const players = {}; // { socketId: { gameId } }
app.prepare().then(() => {
const httpServer = createServer(handler);
const io = new Server(httpServer);
io.on("connection", (socket) => {
console.log(`#${socket.id} connected`);
socket.on("join", (gameId) => {
if (!games[gameId]) {
games[gameId] = { players: [] };
}
games[gameId].players.push(socket.id);
players[socket.id] = { gameId };
// Inform the other players about the new player
socket.to(gameId).emit("gameEvent", {
type: "playerJoin",
payload: socket.id,
});
socket.join(gameId);
console.log(`#${socket.id} joined @${gameId}`);
});
socket.on("gameEvent", (event, gameId) => {
console.log(`gameEvent @${gameId} ->`, event);
socket.to(gameId).emit("gameEvent", event);
});
socket.on("disconnect", () => {
console.log(`#${socket.id} disconnected`);
const { gameId } = players[socket.id];
if (gameId) {
console.log(`#${socket.id} left @${gameId}`);
const game = games[gameId];
game.players = game.players.filter((player) => player !== socket.id);
if (game.players.length === 0) {
delete games[gameId];
}
socket.to(gameId).emit("gameEvent", {
type: "playerLeave",
payload: socket.id,
});
}
delete players[socket.id];
});
});
httpServer
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});