-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
95 lines (83 loc) · 2.18 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require('dotenv').config();
const express = require('express');
const path = require('path');
const OpenTok = require('opentok');
const port = process.env.port || 3000;
const app = express();
const opentok = new OpenTok(process.env.API_KEY, process.env.API_SECRET);
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(`${__dirname}/public`));
app.get('/', (req, res) => {
res.sendFile(path.join(`${__dirname}/public/index.html`));
});
app.get('/session/:sessionId', (req, res) => {
res.sendFile(path.join(`${__dirname}/public/call.html`));
});
app.post('/api/create', (req, res) => {
opentok.createSession(
{
mediaMode: 'routed',
},
(err, session) => {
if (err) {
res.status(500).send({ error: 'createSession error' });
return;
}
res.setHeader('Content-Type', 'application/json');
res.send({
sessionId: session.sessionId,
});
},
);
});
app.post('/api/credentials', (req, res) => {
const { sessionId } = req.body;
const token = opentok.generateToken(sessionId);
res.send({
sessionId,
apiKey: process.env.API_KEY,
token,
});
});
app.post('/api/archive/start/:sessionId', (req, res) => {
const { sessionId } = req.params;
const time = new Date().toLocaleString();
const archiveOptions = {
name: `archive-${time}`,
outputMode: 'composed',
layout: {
type: 'bestFit',
screenshareType: 'pip',
},
};
opentok.startArchive(sessionId, archiveOptions, (err, archive) => {
if (err) {
return res.status(500).send(err);
}
return res.json(archive);
});
});
app.post('/api/archive/:archiveId/stop', (req, res) => {
const { archiveId } = req.params;
opentok.stopArchive(archiveId, (err, archive) => {
if (err) {
return res.status(500).send({
archiveId,
error: err,
});
}
return res.json(archive);
});
});
app.get('/api/archive/list', (req, res) => {
opentok.listArchives({ count: 10 }, (err, archive) => {
if (err) {
return res.status(500).send(err);
}
return res.json(archive);
});
});
app.listen(port, () => {
console.log(`App running on port: ${port}`);
});