-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
150 lines (120 loc) · 4.53 KB
/
Copy pathserver.js
File metadata and controls
150 lines (120 loc) · 4.53 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
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
const express = require("express");
const { Server } = require("socket.io");
const { v4: uuidV4 } = require("uuid");
const app = express(); // initialize express
const port = process.env.PORT || 8080;
// io.connection
const server = app.listen(port, () => {
console.log(`listening on *:${port}`);
});
const io = new Server(server, {
cors: "*", // allow connection from any origin
});
const rooms = new Map();
io.on("connection", (socket) => {
console.log(socket.id);
socket.on("username", (username) => {
console.log("username:", username);
socket.data.username = username;
});
socket.on("createRoom", async (callback) => {
const roomId = uuidV4(); // <- 1 create a new uuid
await socket.join(roomId); // <- 2 make creating user join the room
// set roomId as a key and roomData including players as value in the map
rooms.set(roomId, {
roomId,
players: [{ id: socket.id, username: socket.data?.username }],
});
// returns Map(1){'2b5b51a9-707b-42d6-9da8-dc19f863c0d0' => [{id: 'socketid', username: 'username1'}]}
callback(roomId); // <- 4 respond with roomId to client by calling the callback function from the client
});
socket.on("move", (data) => {
// emit to all sockets in the room except the emitting socket.
socket.to(data.room).emit("move", data.move);
});
socket.on("closeRoom", async (data) => {
socket.to(data.roomId).emit("closeRoom", data); // <- 1 inform others in the room that the room is closing
const clientSockets = await io.in(data.roomId).fetchSockets(); // <- 2 get all sockets in a room
// loop over each socket client
clientSockets.forEach((s) => {
s.leave(data.roomId); // <- 3 and make them leave the room on socket.io
});
rooms.delete(data.roomId); // <- 4 delete room from rooms map
});
socket.on("disconnect", () => {
const gameRooms = Array.from(rooms.values()); // <- 1
gameRooms.forEach((room) => {
// <- 2
const userInRoom = room.players.find((player) => player.id === socket.id); // <- 3
if (userInRoom) {
if (room.players.length < 2) {
// if there's only 1 player in the room, close it and exit.
rooms.delete(room.roomId);
return;
}
socket.to(room.roomId).emit("playerDisconnected", userInRoom); // <- 4
}
});
});
socket.on("offer", (payload) => {
io.to(payload.target).emit("offer", payload);
});
socket.on("answer", (payload) => {
io.to(payload.target).emit("answer", payload);
});
socket.on("ice-candidate", (incoming) => {
io.to(incoming.target).emit("ice-candidate", incoming.candidate);
});
socket.on("message", ({ message, room }) => {
const obj = {
message,
id: socket.id,
};
socket.to(room).emit("message_send", obj);
});
socket.on("joinRoom", async (args, callback) => {
const room = rooms.get(args.roomId);
let error, message;
if (!room) {
error = true;
message = "room does not exist";
} else if (room.length <= 0) {
error = true;
message = "room is empty";
} else if (room.length >= 2) {
error = true;
message = "room is full"; // set message to 'room is full'
}
if (error) {
// if there's an error, check if the client passed a callback,
// call the callback (if it exists) with an error object and exit or
// just exit if the callback is not given
if (callback) {
// if user passed a callback, call it with an error payload
callback({
error,
message,
});
}
return; // exit
}
await socket.join(args.roomId); // make the joining client join the room
// const otherUser = rooms[args.roomId].find(id => id !== socket.id);
// if (otherUser) {
// socket.emit("other user", otherUser);
// socket.to(otherUser).emit("user joined", socket.id);
// }
// add the joining user's data to the list of players in the room
const roomUpdate = {
...room,
players: [
...room.players,
{ id: socket.id, username: socket.data?.username },
],
};
rooms.set(args.roomId, roomUpdate);
callback(roomUpdate); // respond to the client with the room details.
// emit an 'opponentJoined' event to the room to tell the other player that an opponent has joined
socket.to(args.roomId).emit("opponentJoined", roomUpdate);
});
});