-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (32 loc) · 1.37 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
const WEB_SOCKET_PORT = process.env.WEB_SOCKET_PORT || 8080;
// Websocket server configuration
const WebSocket = require("ws");
const WebSocketServer = new WebSocket.Server({ port: WEB_SOCKET_PORT });
const { compileAndUploadSketch } = require("./controllers/sketchControllers");
//const { randomizeLEDs } = require("./controllers/randomizeLEDs");
const { testFunctions } = require("./challengesTests/challengesTests");
const { randomizeLEDs } = require("./controllers/randomizeLEDs");
WebSocketServer.on("connection", (ws) => {
console.log("New client connected");
ws.on("message", async (data) => {
// get the challengeId and code from the client (editor or missing led module)
const { challengeId, code } = JSON.parse(data);
// handle randomize leds message from the missing led module
if (challengeId === "randomize-leds") {
randomizeLEDs(ws);
} else {
// Get the test function for the challenge if available
const testSketch = testFunctions(challengeId, code);
// Run the test if available, otherwise compile and upload the user's sketch
if (testSketch) {
compileAndUploadSketch(testSketch, ws, true);
} else {
compileAndUploadSketch(code, ws, false);
}
}
});
ws.on("close", () => {
console.log("Client disconnected");
});
});
console.log(`Websocket server started at port ${WEB_SOCKET_PORT}`);