forked from opentok/broadcast-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (66 loc) · 2.13 KB
/
index.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
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(express.static('public'));
app.set('view engine', 'ejs');
const opentok = require('./services/opentok-api');
/*
* Routes
*/
app.get('/', (req, res) => {
res.redirect('/viewer');
});
app.get('/viewer', async (req, res) => {
opentok.getCredentials('viewer')
.then(credentials => res.render('pages/viewer', { credentials: JSON.stringify(credentials) }))
.catch(error => res.status(500).send(error));
})
app.get('/host', (req, res) => {
opentok.getCredentials('host')
.then(credentials => res.render('pages/host', { credentials: JSON.stringify(credentials) }))
.catch(error => res.status(500).send(error));
});
app.get('/guest', (req, res) => {
opentok.getCredentials('guest')
.then(credentials => res.render('pages/guest', { credentials: JSON.stringify(credentials) }))
.catch(error => res.status(500).send(error));
});
app.get('/broadcast', (req, res) => {
const url = req.query.url;
const availableAt = req.query.availableAt;
res.render('pages/broadcast', { broadcast: JSON.stringify({ url, availableAt }) });
});
app.get('*', (req, res) => {
res.redirect('/viewer');
});
/*
* API Endpoints
*/
app.post('/broadcast/start', (req, res) => {
const { streams, rtmp } = req.body;
opentok.startBroadcast(streams, rtmp)
.then(data => res.send(data))
.catch(error => res.status(500).send(error));
});
app.post('/broadcast/layout', (req, res) => {
const { streams, type } = req.body;
opentok.updateLayout(streams, type)
.then(data => res.status(200).send({}))
.catch(error => res.status(500).send(error));
});
app.post('/broadcast/classes', (req, res) => {
const { classList } = req.body;
opentok.updateStreamClassList(classList)
.then(data => res.status(200).send({}))
.catch(error => res.status(500).send(error));
});
app.post('/broadcast/end', (req, res) => {
opentok.stopBroadcast()
.then(data => res.send(data))
.catch(error => res.status(500).send(error));
});
/*
* Listen
*/
app.listen(port, () => console.log(`app listening on port ${port}`));