Fix WebSocket, add auto refresh on frontend update
This commit is contained in:
3
config.json
Normal file
3
config.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"port": 8080
|
||||||
|
}
|
||||||
20
ejs/footer.ejs
Normal file
20
ejs/footer.ejs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<script>
|
||||||
|
let ws;
|
||||||
|
|
||||||
|
function connectWebSocket() {
|
||||||
|
const ws = new WebSocket((window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + window.location.pathname);
|
||||||
|
|
||||||
|
|
||||||
|
ws.onmessage = async (event) => {
|
||||||
|
if (event.data === 'update') {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onclose = () => {
|
||||||
|
setTimeout(connectWebSocket, 5000);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
connectWebSocket();
|
||||||
|
</script>
|
||||||
57
ejs/home.ejs
Normal file
57
ejs/home.ejs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset='utf-8'>
|
||||||
|
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
||||||
|
<title>XernoBomb</title>
|
||||||
|
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||||
|
<style>
|
||||||
|
@import url('https://fonts.googleapis.com/css2?family=Fredoka:wght@300..700&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
|
||||||
|
|
||||||
|
* {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
box-sizing: border-box;
|
||||||
|
outline: none;
|
||||||
|
font-family: 'Poppins', sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
background-color: #111;
|
||||||
|
color: white;
|
||||||
|
position: absolute;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.home {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
text-align: center;
|
||||||
|
gap: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
background-color: #505050;
|
||||||
|
color: white;
|
||||||
|
padding: 10px;
|
||||||
|
font-size: 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
border-radius: 5px;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="home">
|
||||||
|
<h1>XernoBomb</h1>
|
||||||
|
<a href="/create">Create lobby</a>
|
||||||
|
<a href="/lobbies">Search Lobbies</a>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
<%- include('footer') %>
|
||||||
|
</html>
|
||||||
33
index.js
33
index.js
@@ -1,15 +1,44 @@
|
|||||||
const express = require('express');
|
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');
|
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.use(express.json());
|
||||||
app.set('view engine', 'ejs');
|
app.set('view engine', 'ejs');
|
||||||
app.set('views', path.join(__dirname, 'ejs'));
|
app.set('views', path.join(__dirname, 'ejs'));
|
||||||
|
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.render('home');
|
res.render('home');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(config.port, () => {
|
server.listen(config.port, () => {
|
||||||
console.log(`Server started on port ${config.port}`);
|
console.log(`Server started on port ${config.port}`);
|
||||||
});
|
});
|
||||||
8
package.json
Normal file
8
package.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"ejs": "^3.1.10",
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"fs": "^0.0.1-security",
|
||||||
|
"ws": "^8.18.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user