-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (57 loc) · 1.99 KB
/
index.js
File metadata and controls
69 lines (57 loc) · 1.99 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
/***************************************************************************************
* used code example:
* Title: Socket.io tutorial: Real-time communication in web development
* Author: Mateusz Piguła
* Date: 30 April, 2020
* Availability: https://tsh.io/blog/socket-io-tutorial-real-time-communication/
***************************************************************************************/
const express = require("express")
const socket = require("socket.io")
const PORT = 5000;
const app = express();
const server = app.listen(PORT, function () {
console.log(`Listening on port ${PORT}`);
console.log(`http://localhost:${PORT}`);
});
// Static files
app.use(express.static("public"));
// Socket setup
const io = socket(server);
const activeUsers = new Set()
var shapes = []
var idCounter = 0
io.on("connection", function (socket) {
console.log("Made socket connection");
socket.on("new user", function (data) {
socket.userId = data;
activeUsers.add(data);
io.emit("new user", [...activeUsers]);
socket.emit("whiteboard", shapes); // send whiteboard to new user
});
socket.on("disconnect", () => {
activeUsers.delete(socket.userId);
io.emit("user disconnected", socket.userId);
});
socket.on("create shape", function (data) {
let shape = {...data, id: `shape${idCounter++}`}
shapes = [...shapes, shape]
io.emit("create shape", shape)
})
socket.on("delete shape", function (shapeId) {
var index = shapes.findIndex(s => s.id == shapeId)
if (index!=-1) {
shapes = [...shapes.slice(0,index), ...shapes.slice(index+1)]
io.emit("delete shape", shapeId)
}
})
socket.on("move shape", function (data) {
const {id, position} = data
var index = shapes.findIndex(s => s.id == id)
if (index!==-1) {
shapes = [...shapes.slice(0,index), {...shapes[index], "position":position}, ...shapes.slice(index+1)]
io.emit("move shape", data)
} else {
socket.emit("refresh")
}
})
});