-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
128 lines (102 loc) · 3.19 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const express = require('express')
const app = express();
const fs = require('fs');
const { v4: uuidV4 } = require('uuid');
const https = require('https');
const options = {
key: fs.readFileSync('ssl/key.pem'),
cert: fs.readFileSync('ssl/cert.pem')
};
const server = https.createServer(options, app);
const { WebSocketServer } = require('ws');
const wss = new WebSocketServer({ server });
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.json()); // parse application/json
app.use(express.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
app.use(express.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// });
// app.use(function(req, res, next) {
// console.log(req.url);
// if (req.url.search("peerjs") !== -1) {
// // console.log(req.url);
// // let url = req.url.slice(7);
// // console.log(url);
// res.redirect(`http://localhost:3001${req.url}`);
// }
// next();
// });
app.get('/', (req, res) => {
res.redirect(`/${uuidV4()}`);
});
// app.get('/:room', (req, res) => {
// res.render('room', {
// roomId: req.params.room
// })
// });
app.get('/admin/:room', (req, res) => {
return res.render('admin', {
roomId: req.params.room,
username: "Admin",
key: options.key,
cert: options.cert
});
});
app.get('/client/:username/:room', (req, res) => {
return res.render('client', {
roomId: req.params.room,
username: req.params.username,
key: options.key,
cert: options.cert
});
});
wss.on('connection', function connection(socket)
{
socket.on('message', function incoming(message)
{
let msg = JSON.parse(message);
if (msg.event === "join-room")
{
// if (msg.username !== "Admin")
// {
// }
console.log("yes");
// socket.send(JSON.stringify({
// event: 'user-connected',
// username: msg.username,
// userId: msg.userId
// }));
socket.send(JSON.stringify({
event: 'hello'
}));
socket.on('close', function()
{
socket.send(JSON.stringify({
event: 'user-disconnected',
userId: msg.userId
}));
});
}
else if (msg.event === "admin ended the call")
{
socket.send(JSON.stringify({
event: "admin ended the call",
endedUserId: msg.endedUserId
}));
}
else if (msg.event === "user ended the call")
{
socket.send(JSON.stringify({
event: "user ended the call",
endedUserId: msg.endedUserId
}));
}
});
});
server.listen(3000);
console.log("Server started listening on port 3000");
require('./peer-server.js');