Last active
March 26, 2021 14:44
-
-
Save LiorB-D/cbe3d6fdbef40b69068249b0ff073d9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create our express and socket.io servers | |
const express = require('express') | |
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') // Tell Express we are using EJS | |
app.use(express.static('public')) // Tell express to pull the client script from the public folder | |
// If they join the base link, generate a random UUID and send them to a new room with said UUID | |
app.get('/', (req, res) => { | |
res.redirect(`/${uuidV4()}`) | |
}) | |
// If they join a specific room, then render that room | |
app.get('/:room', (req, res) => { | |
res.render('room', {roomId: req.params.room}) | |
}) | |
// When someone connects to the server | |
io.on('connection', socket => { | |
// When someone attempts to join the room | |
socket.on('join-room', (roomId, userId) => { | |
socket.join(roomId) // Join the room | |
socket.broadcast.emit('user-connected', userId) // Tell everyone else in the room that we joined | |
// Communicate the disconnection | |
socket.on('disconnect', () => { | |
socket.broadcast.emit('user-disconnected', userId) | |
}) | |
}) | |
}) | |
server.listen(3000) // Run the server on the 3000 port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please add a space between
//
and a comment.