forked from pubnub/auth-chat-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxirsys.js
89 lines (88 loc) · 3.47 KB
/
xirsys.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
var https = require('https');
var config = require('./config');
//var JSON = require('JSON');
/*function xirsys () {
return {
info: {ident: 'noev', secret: '9bb9eb0e-3f01-11e7-bf11-c9222b51e852', channel: 'channel1'},
turn: function (req, res, next) {
//if error
if (req.error) {
console.log('error: ', req.error);
delete req.user;
next();
}
//if error null proxy request to xirsys
else {
var options = {
method: 'PUT',
host: 'global.xirsys.net',
path: '/_turn' + '/' + xirsys.info.channel,
headers: {
"Authorization": "Basic " + new Buffer(xirsys.info.ident + ":" + xirsys.info.secret).toString("base64")
}
};
//make call to Xirsys API, with modified request. Expect and return response to client.
https.request(options, function (httpres) {
var str = '';
httpres.on('data', function (data) {
str += data;
});
//error - returns 500 status and formatted response
httpres.on('error', function (e) {
console.log('error: ', e);
delete req.user;
next();
});
httpres.on('end', function () {
var ics = JSON.parse(str);
//console.log(ics.v.iceServers);
req.user.xirsys = JSON.stringify({iceServers: ics.v.iceServers}, null, null);
next();
});
}).end();
}
}
}
}*/
// export the class
var own = {
info: {ident: 'noev', secret: '9bb9eb0e-3f01-11e7-bf11-c9222b51e852', channel: 'channel1'},
turn: function (req, res, next) {
if (req.error) {
console.log('error: ', req.error);
delete req.user;
next();
}
//if error null proxy request to xirsys
else {
var options = {
method: 'PUT',
host: 'global.xirsys.net',
path: '/_turn' + '/' + own.info.channel,
headers: {
"Authorization": "Basic " + new Buffer(own.info.ident + ":" + own.info.secret).toString("base64")
}
};
//make call to Xirsys API, with modified request. Expect and return response to client.
https.request(options, function (httpres) {
var str = '';
httpres.on('data', function (data) {
str += data;
});
//error - returns 500 status and formatted response
httpres.on('error', function (e) {
console.log('error: ', e);
delete req.user;
next();
});
httpres.on('end', function () {
var ics = JSON.parse(str);
//console.log(ics.v.iceServers);
req.user.xirsys = JSON.stringify({iceServers: ics.v.iceServers}, null, null);
next();
});
}).end();
}
}
};
module.exports = own;