-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (92 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import express from 'express';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { Server } from 'socket.io';
import crypto from "crypto";
import session from "express-session";
import cookieParser from 'cookie-parser';
import dotenv from "dotenv";
dotenv.config();
const app = new express();
const server = createServer(app);
const io = new Server(server, {
connectionStateRecovery: {}
});
const port = process.env.PORT || 3000;
const __dirname = dirname(fileURLToPath(import.meta.url));
function getUserRooms(socket) {
return Object.entries(rooms).reduce((IDs, [roomID, userDat]) => {
if (userDat.users[socket.id] != null) IDs.push(roomID)
return IDs;
}, [])
}
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json({
type: ['application/json', 'text/plain']
}));
app.use(session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: {
secure: true,
}
}));
app.use(cookieParser());
const rooms = {};
app.get('/',
(req, res, next) => {
if (!req.session.user) {
if (!req.cookies.user) {
req.session.user = `Player${Math.floor(1000 + Math.random() * 8999)}`;
return next();
}
req.session.user = req.cookies.user;
}
return next();
},
(req, res) => {
res.cookie("user", req.session.user);
res.sendFile(join(__dirname, 'homepage.html'));
});
app.post('/usernameUpdate', (req, res) => {
req.session.user = req.body.username;
res.clearCookie("user");
res.cookie("user", req.session.user);
res.send();
});
app.get('/create-room', (req, res) => {
let roomID = crypto.randomBytes(4).toString('hex');
if (rooms[roomID] != null) {
roomID = crypto.randomBytes(4).toString('hex');
}
rooms[roomID] = { users: {} };
res.redirect(roomID);
});
app.get('/:room', (req, res) => {
if (rooms[req.params.room] == null) {
return res.redirect('/');
}
res.sendFile(join(__dirname, 'chatroom.html'));
});
io.on("connection", (socket) => {
socket.on('new-user', (roomID, name) => {
socket.join(roomID);
rooms[roomID].users[socket.id] = name;
socket.broadcast.to(roomID).emit('user-connected', name);
});
socket.on("send-chat-message", (roomID, msg) => {
socket.broadcast.to(roomID).emit("chat-message", { message: msg, username: rooms[roomID].users[socket.id] });
});
socket.on("disconnect", () => {
getUserRooms(socket).forEach(roomID => {
socket.broadcast.to(roomID).emit("user-disconnected", rooms[roomID].users[socket.id]);
delete rooms[roomID].users[socket.id];
});
});
});
server.listen(port, () => {
console.log(`server running at http://localhost:${port}`);
});