initial
This commit is contained in:
96
server/server.js
Normal file
96
server/server.js
Normal file
@@ -0,0 +1,96 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const https = require("https");
|
||||
const express = require("express");
|
||||
const WebSocket = require("ws");
|
||||
|
||||
const config = require("./config/config");
|
||||
const FluidNCClient = require("./fluidnc/FluidNCClient");
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.static(path.join(__dirname,"../web")));
|
||||
|
||||
const server = https.createServer({
|
||||
key: fs.readFileSync(path.join(__dirname,"../cert/key.pem")),
|
||||
cert: fs.readFileSync(path.join(__dirname,"../cert/cert.pem"))
|
||||
},app);
|
||||
|
||||
const wss = new WebSocket.Server({server});
|
||||
|
||||
const fluid = new FluidNCClient();
|
||||
|
||||
let clients = [];
|
||||
|
||||
wss.on("connection",(ws)=>{
|
||||
|
||||
console.log("[WS] Client connected");
|
||||
|
||||
clients.push(ws);
|
||||
|
||||
ws.on("message",(msg)=>{
|
||||
|
||||
try{
|
||||
|
||||
const data = JSON.parse(msg);
|
||||
|
||||
console.log("[WS] Received message:", data);
|
||||
|
||||
if(data.type==="jog"){
|
||||
|
||||
fluid.jog(data.axis,data.value);
|
||||
|
||||
}
|
||||
|
||||
if(data.type==="gcode"){
|
||||
|
||||
fluid.sendGcode(data.cmd);
|
||||
|
||||
}
|
||||
|
||||
if(data.type==="zero"){
|
||||
|
||||
fluid.setZero();
|
||||
|
||||
}
|
||||
|
||||
}catch(e){
|
||||
|
||||
console.log("[WS] Error parsing message:", e);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
ws.on("close",()=>{
|
||||
|
||||
clients = clients.filter(c=>c!==ws);
|
||||
|
||||
console.log("[WS] Client disconnected");
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
fluid.onMessage((msg)=>{
|
||||
|
||||
const json = JSON.stringify(msg);
|
||||
|
||||
clients.forEach(c=>{
|
||||
|
||||
if(c.readyState===WebSocket.OPEN){
|
||||
|
||||
c.send(json);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
server.listen(config.server.port,()=>{
|
||||
|
||||
console.log("[Server] Running at:");
|
||||
console.log(`https://localhost:${config.server.port}`);
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user