2024-01-05 23:35:11 +01:00
|
|
|
import express from 'express';
|
|
|
|
import { createServer } from 'node:http';
|
|
|
|
import { Server } from 'socket.io';
|
|
|
|
import path from 'node:path';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
import session from "express-session";
|
|
|
|
import { engine } from 'express-handlebars';
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.engine('handlebars', engine());
|
|
|
|
app.set('view engine', 'handlebars');
|
|
|
|
app.set('views', './views');
|
|
|
|
|
|
|
|
const server = createServer(app);
|
|
|
|
const io = new Server(server);
|
|
|
|
|
2024-01-07 00:59:08 +01:00
|
|
|
app.set('trust proxy', 1);
|
2024-01-05 23:35:11 +01:00
|
|
|
const sessionMiddleware = session({
|
|
|
|
secret: uuidv4(),
|
|
|
|
resave: true,
|
|
|
|
saveUninitialized: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(sessionMiddleware);
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
|
|
|
io.engine.use(sessionMiddleware);
|
|
|
|
|
|
|
|
app.get("/", (req, res) => {
|
2024-01-07 00:59:08 +01:00
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
res.render('index');
|
|
|
|
});
|
|
|
|
|
2024-01-06 21:09:41 +01:00
|
|
|
app.get("/*", (req, res) => {
|
|
|
|
res.redirect("/?path=" + req.originalUrl);
|
2024-01-05 23:35:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
io.on('connection', (socket) => {
|
|
|
|
const session = socket.request.session;
|
|
|
|
|
2024-01-06 21:09:41 +01:00
|
|
|
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
socket.on('chat message', (msg) => {
|
|
|
|
console.log('message: ' + msg);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(7777, () => {
|
|
|
|
console.log('Server running at http://localhost:7777');
|
|
|
|
});
|