-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (31 loc) · 1.05 KB
/
server.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
require("dotenv").config()
const express = require("express")
const { Socket } = require("socket.io")
const app = express()
const server = require("http").Server(app)
const io = require("socket.io")(server)
const {v4: uuidv4} = require("uuid")
app.set("view engine", "ejs")
app.use(express.static("public"))
app.get("/", (req, res)=>{
const freshRoomId = uuidv4()
// res.redirect(`/${freshRoomId}`)
res.render("index", {freshRoomId: freshRoomId})
})
app.get("/:room", (req, res)=>{
// console.log("requested room with id :", req.params.room , typeof(req.params.room))
res.render("room", {roomId: req.params.room})
})
io.on("connection", (socket)=>{
socket.on("join-room", (roomId, userId)=>{
socket.join(roomId)
socket.broadcast.to(roomId).emit("user-connected", userId)
socket.on("disconnect", ()=>{
socket.broadcast.to(roomId).emit("user-disconnected", userId)
})
})
})
const PORT = process.env.PORT || 3000
server.listen(PORT, ()=>{
console.log("Server is running on : ", PORT)
})