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 index 040ec3f..1573675 100644 --- a/ejs/home.ejs +++ b/ejs/home.ejs @@ -53,4 +53,5 @@ Search Lobbies +<%- include('footer') %> \ No newline at end of file diff --git a/index.js b/index.js index ba3365a..0467fc6 100644 --- a/index.js +++ b/index.js @@ -2,13 +2,34 @@ const express = require('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')); @@ -18,6 +39,6 @@ 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/node_modules/.package-lock.json b/node_modules/.package-lock.json index 73af889..1ac1e39 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -412,6 +412,12 @@ "node": ">= 0.8" } }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==", + "license": "ISC" + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/package-lock.json b/package-lock.json index e174037..525ef61 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "dependencies": { "ejs": "^3.1.10", "express": "^5.1.0", + "fs": "^0.0.1-security", "ws": "^8.18.2" } }, @@ -419,6 +420,12 @@ "node": ">= 0.8" } }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==", + "license": "ISC" + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/package.json b/package.json index 48c39e4..cb254f3 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "dependencies": { "ejs": "^3.1.10", "express": "^5.1.0", + "fs": "^0.0.1-security", "ws": "^8.18.2" } }