diff --git a/shell/watcher.js b/shell/watcher.js index ac5ef42..ac742db 100644 --- a/shell/watcher.js +++ b/shell/watcher.js @@ -105,20 +105,46 @@ function calculateDiff(oldValue, newValue) { return changes.join('\n'); } -//// WEBSOCKET SERVER //// +//// HTTP SERVER //// const WebSocket = require('ws'); const http = require('http'); const url = require('url'); +const path = require('path'); +const hostname = '0.0.0.0'; +const port = 1024; -// Create an HTTP server -const websocket_server = http.createServer((req, res) => { - res.writeHead(404); - res.end(); +const server = http.createServer((req, res) => { + if(req.url === '/') { + // Serve the HTML file + fs.readFile(path.join(__dirname, 'index.html'), (err, data) => { + if(err) { + res.writeHead(500); + res.end('Error loading index.html'); + return; + } + res.writeHead(200, {'Content-Type': 'text/html'}); + res.end(data); + }); + } else { + // Handle 404 + res.writeHead(404); + res.end('Not found'); + } }); +server.listen(port, hostname, () => { + console.log(`Web Server running at http://${hostname}:${port}/`); +}); + + +//// WEBSOCKET SERVER //// +ws_port = port + 1; + // Attach WebSocket server to the HTTP server -const wss = new WebSocket.Server({ noServer: true }); +const wss = new WebSocket.Server({ port: ws_port }); + +console.log(`WebSocket Server running at http://${hostname}:${ws_port}/`); wss.on('connection', function connection(ws) { console.log('WebSocket client connected'); @@ -157,43 +183,6 @@ function send_ws_message(message) { }); } -// Start the HTTP server -const PORT = 3001; // Ensure this port is different from the UNIX socket server if running on the same machine -websocket_server.listen(PORT, () => { - console.log(`WebSocket listening on port ${PORT}`); -}); - -//// HTTP SERVER //// - -const http = require('http'); -const fs = require('fs'); -const path = require('path'); - -const hostname = '0.0.0.0'; -const port = 1024; - -const server = http.createServer((req, res) => { - if(req.url === '/') { - // Serve the HTML file - fs.readFile(path.join(__dirname, 'index.html'), (err, data) => { - if(err) { - res.writeHead(500); - res.end('Error loading index.html'); - return; - } - res.writeHead(200, {'Content-Type': 'text/html'}); - res.end(data); - }); - } else { - // Handle 404 - res.writeHead(404); - res.end('Not found'); - } -}); - -server.listen(port, hostname, () => { - console.log(`Web Server running at http://${hostname}:${port}/`); -}); //// Exit when low disk space /////