-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (59 loc) · 1.93 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
78
79
const { ESRCH } = require("constants");
const express = require("express");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
const { v4: uuidv4 } = require("uuid");
const port = process.env.PORT || 3000;
app.set("view-engine", "ejs");
app.use(express.static("public"));
app.use("/room/:id", express.static("public"));
app.get("/", (req, res) => {
res.render("index.ejs");
});
app.get("/room", (req, res) => {
res.redirect(`/room/${uuidv4()}`);
});
app.get("/room/:id", (req, res) => {
let theroomid = req.params.id;
res.render("room.ejs", { roomId: theroomid, userId: uuidv4() });
});
// app.get('/:room', (req, res) => {
// res.render('room', { roomId: req.params.room })
// })
userIdlist = [];
io.on("connection", (socket) => {
socket.on("join-room", (roomId, userId) => {
socket.join(roomId);
socket.to(roomId).broadcast.emit("user-connected", userId);
socket.on("disconnect", () => {
socket.to(roomId).broadcast.emit("user-disconnected", userId);
});
});
});
io.on("connection", (socket) => {
socket.on("join-room", (userId, roomId) => {
console.log(`User ${userId} Joined room ${roomId}`);
if (userIdlist.length === 1) {
let admin = userIdlist[0];
console.log("admin: ", admin);
}
userIdlist.push(userId);
socket.join(roomId);
socket.to(roomId).broadcast.emit("new-member", userId);
theroomid = roomId;
console.log("roodID: ", theroomid);
});
// Recieving updates from user
socket.on("update_canvas", function (data) {
// store all drawings indev*
const history = [];
history.push(data);
// socket.emit('update_canvas',item);
for (let item of history) socket.to(theroomid).emit("update_canvas", item);
// send updates to all sockets except sender
socket.to(theroomid).emit("update_canvas", data);
// socket.emit('update_canvas',data);
});
});
server.listen(port);