From eabad4e055b63f5a71eb7e27367795f11b44e867 Mon Sep 17 00:00:00 2001 From: jgj52 Date: Thu, 19 Jun 2025 19:08:41 +0200 Subject: [PATCH] Fix WebSocket, add auto refresh on frontend update --- config.json | 3 +++ ejs/footer.ejs | 20 ++++++++++++++++++ ejs/home.ejs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 33 +++++++++++++++++++++++++++-- package.json | 8 +++++++ 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 config.json create mode 100644 ejs/footer.ejs create mode 100644 ejs/home.ejs create mode 100644 package.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..a7bf442 --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "port": 8080 +} \ No newline at end of file diff --git a/ejs/footer.ejs b/ejs/footer.ejs new file mode 100644 index 0000000..fdb0d99 --- /dev/null +++ b/ejs/footer.ejs @@ -0,0 +1,20 @@ + \ No newline at end of file diff --git a/ejs/home.ejs b/ejs/home.ejs new file mode 100644 index 0000000..1573675 --- /dev/null +++ b/ejs/home.ejs @@ -0,0 +1,57 @@ + + + + + + XernoBomb + + + + +
+

XernoBomb

+ Create lobby + Search Lobbies +
+ +<%- include('footer') %> + \ No newline at end of file diff --git a/index.js b/index.js index 4e22938..0467fc6 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,44 @@ const express = require('express'); -const app = express(); +const path = require('path'); +const { WebSocketServer } = require('ws'); +const { createServer } = require('http'); +const fs = require('fs'); + const config = require('./config.json'); +let clients = []; + +const app = express(); +const server = createServer(app); +const wss = new WebSocketServer({ server }); + +wss.on('connection', function connection(ws) { + clients.push(ws); + ws.on('close', () => { + clients.pop(ws); + }); +}); + +// auto refresh on frontend update +let lastUpdateTime = Date.now(); +fs.watch('./ejs/', () => { + const now = Date.now(); + if (now - lastUpdateTime > 50) { + clients.forEach((ws) => { + ws.send('update'); + }); + } + lastUpdateTime = now; +}); app.use(express.json()); app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, 'ejs')); + app.get('/', (req, res) => { res.render('home'); }); -app.listen(config.port, () => { +server.listen(config.port, () => { console.log(`Server started on port ${config.port}`); }); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..cb254f3 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "ejs": "^3.1.10", + "express": "^5.1.0", + "fs": "^0.0.1-security", + "ws": "^8.18.2" + } +}