-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (26 loc) · 1007 Bytes
/
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
const express = require("express");
const app = express();
const port = 1284;
const WebSocket = require("ws");
var currentConnection = null;
var lastPlayers = [];
app.use(express.static("./html/"));
app.get("/update", (req, res) => {
var data = JSON.parse(req.query.data);
console.log(data);
if (data.reset) {
lastPlayers = [];
if (currentConnection) currentConnection.send(JSON.stringify({ reset: true }));
return;
}
if (!data.index && lastPlayers[0] && lastPlayers[0].color != data.color) lastPlayers = [];
lastPlayers[data.index] = data;
if (currentConnection) currentConnection.send(JSON.stringify({ players: lastPlayers, change: data.index }));
});
const server = app.listen(port, () => console.log("Server running"));
const wss = new WebSocket.Server({ server });
wss.on("connection", (ws) => {
currentConnection = ws;
ws.send(JSON.stringify({ players: lastPlayers, first: true }));
ws.on("close", () => currentConnection = null);
});