Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (48 loc) · 1.43 KB
/
Copy pathserver.js
File metadata and controls
62 lines (48 loc) · 1.43 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
const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const cors = require('cors');
const PORT = 3005;
const HOST = "127.0.0.1";
let mainWindow = null;
let commandQueue = [];
function startServer(win) {
mainWindow = win;
const app = express();
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: "*" } });
app.use(cors());
app.use(express.json({ limit: '5mb' }));
let currentData = {
title: "", subtitle: "", time: "", image: "",
running: 2, shuffle: 2, repeat: 2, bassIntensity: 0,
};
app.post("/update", (req, res) => {
try {
currentData = req.body;
if (mainWindow) {
mainWindow.webContents.send('update-data', currentData);
}
io.emit("update", currentData);
res.sendStatus(200);
} catch (error) {
res.sendStatus(500);
}
});
app.get("/poll", (req, res) => {
if (commandQueue.length > 0) {
const command = commandQueue.shift();
res.json(command);
} else {
res.json({});
}
});
io.on("connection", (socket) => {
socket.emit("update", currentData);
});
server.listen(PORT, HOST);
}
function addElectronCommand(command) {
commandQueue.push(command);
}
module.exports = { startServer, addElectronCommand };