-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (80 loc) · 2.29 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
let express = require("express");
let app = express();
let httpServer = require("http").createServer(app);
let io = require("socket.io")(httpServer);
let connections = [];
io.on("connect", (socket) => {
connections.push(socket);
console.log(`${socket.id} has connected`);
connections[0].emit("getCanvas")
console.log("sentgetCanvas to client")
io.on("sentCanvas", (data) => {
socket.emit("canvas-data", data )
})
socket.on("propogate", (data) => {
connections.map((con) => {
if (con.id !== socket.id) {
con.emit("onpropogate", data);
}
});
});
//receive and send an image of entire canvas
socket.on("canvas-data", (data) => {
connections.forEach((con) => {
if (con.id != socket.id){
con.emit("canvas-data", data);
}
})
console.log("canvas data recieved")
})
socket.on("draw", (data) => {
connections.forEach((con) => {
if (con.id !== socket.id) {
con.emit("ondraw", {x: data.x , y: data.y});
}
});
});
//emit brush size data
socket.on("widthChange", (data) => {
console.log("width change recieved")
connections.forEach((con)=>{
if (con.id !== socket.id){
con.emit("widthChange", {width: data.width})
}
})
console.log(data.width)
});
//emit colour change of brush
socket.on("colourChange", (data) =>{
console.log("colourChange recieved")
connections.forEach((con)=>{
if (con.id !== socket.id){
con.emit("colourChange", {colour: data.colour})
}
})
console.log("colourChange emitted")
});
//emit message to clear canvas
socket.on("canvasClear", () => {
console.log("clear canvas recieved")
connections.forEach((con)=>{
if (con.id !== socket.id){
con.emit("canvasClear")
}
})
})
socket.on("down", (data) => {
connections.forEach((con) => {
if (con.id !== socket.id) {
con.emit("ondown", {x: data.x , y: data.y});
}
});
});
socket.on("disconnect", (reason) => {
console.log(`${socket.id} is disconnected`);
connections = connections.filter((con) => con.id !== socket.id);
});
});
app.use(express.static("public"));
let PORT = process.env.PORT || 8080;
httpServer.listen(PORT, () => console.log(`Server started on port ${PORT}`));