-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (52 loc) · 1.65 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
const express = require('express');
const {RtcTokenBuilder, RtcRole} = require('agora-access-token');
const PORT = 8080;
const APP_ID = "3b959c49bd68467e9babbb4d10f76a58";
const APP_CERTIFICATE = "c8fcf9cadd42445b9b9553516aead2a4";
const app = express();
const nocache = (req, resp, next) => {
resp.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
resp.header('Expires', '-1');
resp.header('Pragma', 'no-cache');
next();
}
const generateAccessToken = (req, resp) => {
// set response header
resp.header('Acess-Control-Allow-Origin', '*');
// get channel name
const channelName = req.query.channelName;
if (!channelName) {
return resp.status(500).json({ 'error': 'channel is required' });
}
// get uid
let uid = req.query.uid;
if(!uid || uid == '') {
uid = 0;
}
// get role
let role = RtcRole.SUBSCRIBER;
if (req.query.role == 'publisher') {
role = RtcRole.PUBLISHER;
}
// get the expire time
let expireTime = req.query.expireTime;
if (!expireTime || expireTime == '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
const token = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
// return the token
return resp.json({ 'token': token });
}
app.get('/access_token', nocache, generateAccessToken);
app.get('/', function(req, res){
res.send({ title: token });
});
app.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});