forked from Abhishek07Kalra/VideoCall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (55 loc) · 1.58 KB
/
server.js
File metadata and controls
62 lines (55 loc) · 1.58 KB
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
const express = require('express');
const app = express();
const fs = require('fs');
// cat chain cert public cert > sslcert/server.key
var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
var credentials = { key: privateKey, cert: certificate };
const server = require('https').createServer(credentials, app);
const io = require('socket.io')(server);
const port = process.env.PORT || 8443;
const { v4: uuidv4 } = require('uuid');
const { ExpressPeerServer } = require('peer')
const peer = ExpressPeerServer(server, {
debug: true
});
const secret = "<token>"
let rooms = []
//Routes
app.use('/peerjs', peer);
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendStatus('403');
});
app.get('/join/:room', (req, res) => {
if (rooms.includes(req.params.room)) {
res.render('index', { RoomId: req.params.room });
} else {
res.sendStatus('403');
};
});
app.get('/create/:secret', (req, res) => {
if (req.params.secret == secret) {
uuid = uuidv4();
rooms.push(uuid);
//res.send(uuid);
res.redirect('/join/' + uuid);
} else {
res.sendStatus('403');
};
});
//Socket
io.on("connection", (socket) => {
socket.on('newUser', (id, room) => {
socket.join(room);
socket.to(room).broadcast.emit('userJoined', id);
socket.on('disconnect', () => {
socket.to(room).broadcast.emit('userDisconnect', id);
})
})
})
//Start server
server.listen(port, () => {
console.log("Server running on port : " + port);
})