-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (85 loc) · 2.6 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
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
const express = require("express");
const ws = require("ws");
const exphbs = require('express-handlebars');
const app = express()
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
app.use(express.static("static"));
app.use(
express.urlencoded({
extended: true
})
)
app.use(express.json())
const server = app.listen(7000, () => {
console.log(`Express running → PORT ${server.address().port}`);
});
app.get('/', (_, res) => {
res.render("home");
});
app.get('/place', (_, res) => {
res.render("place");
});
app.get('/data', (_, res) => {
res.render("data");
});
app.get('/wallet', (_, res) => {
res.render("wallet");
});
app.get('/faq', (_, res) => {
res.render("faq")
});
const wss = new ws.WebSocketServer({ server });
let pixelPool = {};
let buffered = [];
wss.on("connection", (connection) => {
// On initial connection, send current pixel pool
if (Object.keys(pixelPool).length > 0) {
connection.send(JSON.stringify(
Array.from(
Object.values(pixelPool), x => (
{ status: "add", pixel: x }
))
));
}
// For every pixel received, broadcast it
connection.on("message", (data) => {
let incoming = JSON.parse(data.toString());
if (Array.isArray(incoming)) {
for (const current of incoming) {
if (current.status === "add") {
pixelPool[`${current.pixel.x}${current.pixel.y}`] = current.pixel;
// buffered.push({d: current, conn: connection});
// broadcast(JSON.stringify(current), connection);
} else if (current.status === "del") {
delete current[`${current.pixel.x}${current.pixel.y}`];
// buffered.push({d: current, conn: connection});
} else {
console.log("Wrong status");
return;
}
}
broadcast(JSON.stringify(incoming), connection);
}
})
});
// setInterval(_ => {
// if (buffered.length > 0) {
// broadcast(JSON.stringify(buffered));
// buffered = [];
// }
// }, 400);
function broadcast(msg, connection) {
wss.clients.forEach(client => {
if (client !== connection && client.readyState === ws.WebSocket.OPEN) {
client.send(msg);
}
});
}
function broadcast(msg) {
wss.clients.forEach(client => {
if (client.readyState === ws.WebSocket.OPEN) {
client.send(msg);
}
});
}