-
Notifications
You must be signed in to change notification settings - Fork 13
/
TokenServer.js
executable file
·66 lines (51 loc) · 2.22 KB
/
TokenServer.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
//var fs = require('fs');
//var https = require('https');
var http = require('http');
var express = require('express');
var {RtcTokenBuilder, RtmTokenBuilder, RtcRole, RtmRole} = require('agora-access-token')
var PORT = 8080;
// Fill the appID and appCertificate key given by Agora.io
var appID = "<YOUR APP ID>";
var appCertificate = "<YOUR APP CERTIFICATE>";
// token expire time, hardcode to 3600 seconds = 1 hour
var expirationTimeInSeconds = 3600
var role = RtcRole.PUBLISHER
var app = express();
app.disable('x-powered-by');
app.set('port', PORT);
app.use(express.favicon());
app.use(app.router);
var generateRtcToken = function(req, resp) {
var currentTimestamp = Math.floor(Date.now() / 1000)
var privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
var channelName = req.query.channelName;
// use 0 if uid is not specified
var uid = req.query.uid || 0
if (!channelName) {
return resp.status(400).json({ 'error': 'channel name is required' }).send();
}
var key = RtcTokenBuilder.buildTokenWithUid(appID, appCertificate, channelName, uid, role, privilegeExpiredTs);
resp.header("Access-Control-Allow-Origin", "*")
//resp.header("Access-Control-Allow-Origin", "http://ip:port")
return resp.json({ 'key': key }).send();
};
var generateRtmToken = function(req, resp) {
var currentTimestamp = Math.floor(Date.now() / 1000)
var privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
var account = req.query.account;
if (!account) {
return resp.status(400).json({ 'error': 'account is required' }).send();
}
var key = RtmTokenBuilder.buildToken(appID, appCertificate, account, RtmRole, privilegeExpiredTs);
resp.header("Access-Control-Allow-Origin", "*")
//resp.header("Access-Control-Allow-Origin", "http://ip:port")
return resp.json({ 'key': key }).send();
};
app.get('/rtcToken', generateRtcToken);
app.get('/rtmToken', generateRtmToken);
http.createServer(app).listen(app.get('port'), function() {
console.log('AgoraSignServer starts at ' + app.get('port'));
});
//https.createServer(credentials, app).listen(app.get('port') + 1, function() {
// console.log('AgoraSignServer starts at ' + (app.get('port') + 1));
//});