Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
264 lines (219 loc) · 6.72 KB
/
Copy pathapp.js
File metadata and controls
264 lines (219 loc) · 6.72 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const express = require("express");
const app = express();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const path = require("path");
const PORT = process.env.PORT || 3000;
const MAX_USER_NAME_LENGTH = 32;
const MAX_CANVAS_SIZE = 10000;
app.disable("x-powered-by");
app.use(express.static("./dist"));
function randomString(length, chars, prefix="")
{
let str = "";
for (var i = 0; i < length; i++)
{
const charIndex = Math.floor(Math.random() * (chars.length - 1));
str += chars.charAt(charIndex);
}
return prefix + str;
}
// default room name
function randomRoomName()
{
const nameLength = 6;
const chars = "abcdefghijklmnopqrstuvwxyz1234567890";
return randomString(nameLength, chars);
}
// default user name
function randomUserName()
{
const nameLength = 4;
const chars = "1234567890";
return randomString(nameLength, chars, "user");
}
function getRoomNameFromUrl(url)
{
if (url == null) // it is somehow possible to receive no url
return "unnamed";
const splitArray = url.split("/");
return splitArray[splitArray.length - 1];
}
function getUserNameFromCookie(cookie)
{
if (cookie)
{
const splitArray = cookie.split(/;/);
for (let i = 0; i < splitArray.length; i++)
{
const value = splitArray[i].replace(/^[^=]*=/, "");
const key = splitArray[i].replace(/=.*$/, "").trim();
if (key == "userName")
return validUserName(value);
}
}
return "";
}
function isAdmin(roomName, socketId)
{
return io.sockets.adapter.rooms[roomName].adminId &&
io.sockets.adapter.rooms[roomName].adminId == socketId;
}
function makeAdmin(roomName, socketId)
{
io.sockets.adapter.rooms[roomName].adminId = socketId;
io.to(socketId).emit("setAdmin", true);
}
// makes sure user name is valid
function validUserName(userName)
{
let newName = `${userName}`;
newName = newName.trim();
if (newName.length == 0)
newName = "unnamed user";
else if (newName.length > MAX_USER_NAME_LENGTH)
newName = newName.substring(0, MAX_USER_NAME_LENGTH);
return newName;
}
function userIdToUserObject(id)
{
return {
id: id,
name: io.of("/").sockets[id].userName
};
}
app.get("/d", (req, res) =>
{
res.redirect("/" + randomRoomName());
});
app.get("/draw.html", (req, res) =>
{
res.redirect("/" + randomRoomName());
});
app.get("/:id", (req, res) =>
{
res.sendFile(path.join(__dirname, "./dist/", "draw.html"));
});
io.on("connection", socket =>
{
const url = socket.handshake.headers.referer;
const roomName = getRoomNameFromUrl(url);
let userName = getUserNameFromCookie(socket.handshake.headers.cookie);
if (userName == "") // cookie wasn't set
userName = randomUserName();
socket.userName = userName;
socket.join(roomName);
let userIds = Object.keys(io.sockets.adapter.rooms[roomName].sockets);
userIds = userIds.filter(id => id != socket.id);
const roomUsers = userIds.map(userIdToUserObject);
socket.emit("receiveRoomData", url, roomName, userName, roomUsers);
socket.broadcast.to(roomName).emit("userJoin", userName, socket.id);
if (roomUsers.length <= 0) // this is the first user in this room
{
makeAdmin(roomName, socket.id);
} else
{
// ask for current canvas
if (!io.sockets.adapter.rooms[roomName].requesterIds)
io.sockets.adapter.rooms[roomName].requesterIds = [];
if (!io.sockets.adapter.rooms[roomName].bgRequesterIds)
io.sockets.adapter.rooms[roomName].bgRequesterIds = [];
io.sockets.adapter.rooms[roomName].requesterIds.push(socket.id);
io.to(io.sockets.adapter.rooms[roomName].adminId).emit("canvasRequest");
io.sockets.adapter.rooms[roomName].bgRequesterIds.push(socket.id);
io.to(io.sockets.adapter.rooms[roomName].adminId).emit("backgroundCanvasRequest");
}
socket.on("draw", data =>
{
if (data != null)
socket.broadcast.to(roomName).emit("draw", data);
});
socket.on("receiveCanvas", (canvasData, width, height) =>
{
if (!isAdmin(roomName, socket.id)) // only accept canvas from an admin
return;
if (width < 0 || height < 0 || width > MAX_CANVAS_SIZE || height > MAX_CANVAS_SIZE)
return;
if (io.sockets.adapter.rooms[roomName].requesterIds
&& io.sockets.adapter.rooms[roomName].requesterIds.length > 0)
{
io.sockets.adapter.rooms[roomName].requesterIds.forEach((userId, index, arr) =>
{
io.to(userId).emit("receiveCanvas", canvasData, width, height);
arr.splice(index, 1);
});
}
});
socket.on("receiveBackgroundCanvas", canvasData =>
{
if (!isAdmin(roomName, socket.id)) // only accept canvas from an admin
return;
if (io.sockets.adapter.rooms[roomName].bgRequesterIds
&& io.sockets.adapter.rooms[roomName].bgRequesterIds.length > 0)
{
io.sockets.adapter.rooms[roomName].bgRequesterIds.forEach((userId, index, arr) =>
{
io.to(userId).emit("receiveBackgroundCanvas", canvasData);
arr.splice(index, 1);
});
}
});
// user changed the background
socket.on("receiveBackgroundCanvasAll", canvasData =>
{
socket.broadcast.to(roomName).emit("receiveBackgroundCanvas", canvasData);
});
// user cleared the background
socket.on("backgroundClearAll", () =>
{
socket.broadcast.to(roomName).emit("backgroundClear");
});
socket.on("userNameChange", newUserName =>
{
socket.userName = validUserName(newUserName);
});
socket.on("setCanvasSize", (width, height) =>
{
if (isAdmin(roomName, socket.id))
{
if (width >= 0 && height >= 0 && width <= MAX_CANVAS_SIZE && height <= MAX_CANVAS_SIZE)
socket.broadcast.to(roomName).emit("receiveCanvasSize", width, height);
}
});
socket.on("cursorPosition", (pos, size, color) =>
{
socket.broadcast.to(roomName).emit("cursorPosition", socket.id, pos, size, color);
});
socket.on("disconnect", () =>
{
io.sockets.in(roomName).emit("userLeave", socket.userName, socket.id);
// remove pending canvas request
if (io.sockets.adapter.rooms[roomName] && io.sockets.adapter.rooms[roomName].requesterIds)
{
const index = io.sockets.adapter.rooms[roomName].requesterIds.indexOf(socket.id);
if (index > -1)
io.sockets.adapter.rooms[roomName].requesterIds.splice(index, 1);
}
// remove pending background canvas request
if (io.sockets.adapter.rooms[roomName] && io.sockets.adapter.rooms[roomName].bgRequesterIds)
{
const index = io.sockets.adapter.rooms[roomName].bgRequesterIds.indexOf(socket.id);
if (index > -1)
io.sockets.adapter.rooms[roomName].bgRequesterIds.splice(index, 1);
}
if (io.sockets.adapter.rooms[roomName] && io.sockets.adapter.rooms[roomName].length > 0)
{
// if disconnected user was admin, give admin privileges to the oldest connected user
if (isAdmin(roomName, socket.id))
{
const userIds = Object.keys(io.sockets.adapter.rooms[roomName].sockets);
const newAdminId = userIds[0];
makeAdmin(roomName, newAdminId);
}
}
});
});
http.listen(PORT, () =>
{
console.log(`listening on *:${PORT}`);
});