-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (57 loc) · 1.78 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express')
const app = express()
app.use(express.static(__dirname));
const server = require('http').Server(app)
const io = require('socket.io')(server) //a server based on our express server passed to socket.io
app.set('view engine', 'ejs')
app.enable('trust proxy');
const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server,
{
debug:true,
})
app.use("/peerjs", peerServer)
const{ v4 } = require('uuid')
app.use(express.static('public'))
const users= {};
app.get('/', (request, response)=>
{
//redirect user to a dynamic room
response.redirect(`/${v4()}`) //gives a dynamic url
})
app.get('/:room', (request, response)=>
{
//create a route for our room.
response.render('room', {roomId: request.params.room})
})
io.on('connection', socket=>{
if(!users[socket.id])
{
users[socket.id] = socket.id;
}
socket.on('join-room', (roomId, userId)=>
{
socket.join(roomId)
//notify everyone in the room about the presence of the new user
socket.broadcast.to(roomId).emit('user-joinedin', userId)
//whenever we join a room, the userid and roomid is passed.
socket.on('in-call-message', (msg, user) =>
{
io.to(roomId).emit('new-message', msg, user)
})
socket.on('remove-video', (username)=>
{
io.to(roomId).emit('end-video-chat', username)
})
socket.on('leave-room', (username)=>
{
socket.broadcast.to(roomId).emit('user-left-room', username)
})
socket.on('disconnect', ()=>
{
console.log(roomId+" "+userId)
socket.broadcast.to(roomId).emit('user-left', userId)
})
})
})
server.listen(process.env.PORT || 3000)