This commit is contained in:
Henry Hiles 2022-01-31 10:57:31 -05:00
commit 75840914cf
5 changed files with 322 additions and 117 deletions

View file

@ -4,10 +4,9 @@ const server = require("http").Server(app)
const io = require("socket.io")(server, {
cors: {
origin: [
"http://localhost:5500",
"http://127.0.0.1:5500",
"http//192.168.1.226:5500",
"http//192.168.1.226:5500",
"http://127.0.0.1:3000",
"http://192.168.1.226:3000",
"http://192.168.1.226:3000",
],
},
})
@ -26,10 +25,10 @@ const getUserRooms = (socket) =>
const rooms = {}
app.get("/", (_, res) => {
res.render("index", { rooms: rooms })
res.render("index", { rooms: rooms, roomName: null })
})
app.post("/room", (req, res) => {
app.post("/", (req, res) => {
if (rooms[req.body.room] != null) {
return res.redirect("/")
}
@ -42,7 +41,7 @@ app.get("/:room", (req, res) => {
if (rooms[req.params.room] == null) {
return res.redirect("/")
}
res.render("room", { roomName: req.params.room })
res.render("index", { rooms: rooms, roomName: req.params.room })
})
server.listen(3000)
@ -60,6 +59,13 @@ io.on("connection", (socket) => {
.emit("chat-message", message, rooms[room].users[socket.id])
})
socket.on("name-change", (room, newName) => {
socket.broadcast
.to(room)
.emit("name-changed", rooms[room].users[socket.id], newName)
rooms[room].users[socket.id] = newName
})
socket.on("disconnect", () =>
getUserRooms(socket).forEach((room) => {
socket.broadcast