-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
54 lines (42 loc) · 1.47 KB
/
index.ts
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
import { getInfoObject, setup } from "./src/controller";
import { port } from "./app/src/connectionConfig.json";
import WebSocket from "ws";
import messageHandler from "./src/messageHandler";
import express from "express";
import restApi from "./src/restApi";
const app = express();
const args = process.argv.slice(2);
export const CONFIG = {
ledRefreshRate: args.includes("--refreshRate")
? parseInt(args[args.indexOf("--refreshRate") + 1])
: 50, // default refresh rate
};
if (process.argv.slice(2).includes("--noLeds"))
console.log(
"\x1b[31musing --noLeds flag will log all led chagnes to console instead of applying them to the strips!"
);
const server = app.listen(port, () =>
console.log("\x1b[32mserver running!\x1b[0m")
);
app.use(express.static(__dirname + "/app/build"));
app.use("/api", restApi);
app.get("/effects", (req, res) => {
res.sendFile(__dirname + "/app/build/index.html");
});
app.get("/effects/*", (req, res) => {
res.sendFile(__dirname + "/app/build/index.html");
});
const wsServer = new WebSocket.Server({
server,
});
wsServer.on("connection", (connection) => {
console.log("\x1b[32mconnection\x1b[0m");
connection.on("message", (message) => messageHandler(message, connection));
connection.on("close", () => console.log("\x1b[31mclosed\x1b[0m"));
});
export const sendDataUpdate = () => {
wsServer.clients.forEach((client) => {
client.send(JSON.stringify(getInfoObject()));
});
};
setup();