-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (48 loc) · 1.45 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
68
69
70
71
72
73
74
// Dependencies
const express = require('express');
const app = express();
const { v4: uuid } = require('uuid');
const socketIO = require('socket.io');
const http = require('http');
const expressHTTPServer = http.createServer(app);
const io = new socketIO.Server(expressHTTPServer);
// initialization
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.redirect(`/${uuid()}`)
})
app.get('/:roomId', (req, res) => {
const roomId = req.params.roomId;
res.render('index', {
roomId
})
})
io.on('connection', (socket) => {
// Join A New Room
socket.on('joinRoom', (roomId) => {
socket.join(roomId);
// Notification About New Joining To Active Users
socket.to(roomId).emit('newJoined')
})
// Send Offer
socket.on('sendOffer', (offer, roomId) => {
socket.to(roomId).emit('receiveOffer', offer)
})
// Send Answer
socket.on('sendAnswer', (answer, roomId) => {
socket.to(roomId).emit('receiveAnswer', answer)
})
// Send Ice Candidate
socket.on('iceCandidateSend', (candidate, roomId) => {
socket.to(roomId).emit('iceCandidateReceive', candidate)
})
// Disconnect Event
socket.on('disconnect', () => {
io.emit('someoneLeft', socket.id)
})
})
// Server listening
expressHTTPServer.listen(process.env.PORT || 3000, () => {
console.log("Server has been fucked up on port 3000");
})