2017-06-10 01:44:31 +02:00
|
|
|
import { TokenOptions } from './TokenOptions';
|
2017-06-19 10:16:14 +02:00
|
|
|
import { OpenViduRole } from './OpenViduRole';
|
2017-06-10 01:44:31 +02:00
|
|
|
|
|
|
|
declare const Buffer;
|
|
|
|
declare const require;
|
|
|
|
|
|
|
|
var https = require('https');
|
|
|
|
|
|
|
|
export class Session {
|
|
|
|
|
|
|
|
private sessionIdURL: string = '/api/sessions';
|
|
|
|
private tokenURL: string = '/api/tokens';
|
|
|
|
private sessionId: string = "";
|
|
|
|
private hostname: string;
|
|
|
|
private port: number;
|
|
|
|
|
|
|
|
constructor(private urlOpenViduServer: string, private secret: string) {
|
|
|
|
this.setHostnameAndPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
public getSessionId(callback: Function) {
|
|
|
|
|
|
|
|
if (this.sessionId) {
|
2017-08-29 17:31:34 +02:00
|
|
|
callback(this.sessionId);
|
2017-09-19 17:07:53 +02:00
|
|
|
return;
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
path: this.sessionIdURL,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.getBasicAuth()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
var body = '';
|
|
|
|
res.on('data', (d) => {
|
|
|
|
// Continuously update stream with data
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
// Data reception is done
|
|
|
|
var parsed = JSON.parse(body);
|
|
|
|
this.sessionId = parsed.id;
|
|
|
|
callback(parsed.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
}
|
|
|
|
|
2017-06-19 10:16:14 +02:00
|
|
|
|
|
|
|
public generateToken(callback: Function);
|
|
|
|
public generateToken(tokenOptions: TokenOptions, callback: Function);
|
|
|
|
|
|
|
|
public generateToken(tokenOptions: any, callback?: any) {
|
|
|
|
var requestBody;
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
requestBody = JSON.stringify({
|
|
|
|
'session': this.sessionId,
|
|
|
|
'role': tokenOptions.getRole(),
|
|
|
|
'data': tokenOptions.getData()
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
requestBody = JSON.stringify({
|
|
|
|
'session': this.sessionId,
|
|
|
|
'role': OpenViduRole.PUBLISHER,
|
|
|
|
'data': ''
|
|
|
|
});
|
|
|
|
callback = tokenOptions;
|
|
|
|
}
|
|
|
|
|
2017-06-10 01:44:31 +02:00
|
|
|
var options = {
|
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
path: this.tokenURL,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.getBasicAuth(),
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Length': Buffer.byteLength(requestBody)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
var body = '';
|
|
|
|
res.on('data', (d) => {
|
|
|
|
// Continuously update stream with data
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
// Data reception is done
|
|
|
|
var parsed = JSON.parse(body);
|
|
|
|
callback(parsed.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
req.write(requestBody);
|
|
|
|
req.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
private getBasicAuth() {
|
|
|
|
return 'Basic ' + (new Buffer('OPENVIDUAPP:' + this.secret).toString('base64'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private setHostnameAndPort() {
|
|
|
|
var urlSplitted = this.urlOpenViduServer.split(':');
|
|
|
|
if (urlSplitted.length === 3) { // URL has format: http:// + hostname + :port
|
|
|
|
this.hostname = this.urlOpenViduServer.split(':')[1].replace(/\//g, '');
|
|
|
|
this.port = parseInt(this.urlOpenViduServer.split(':')[2].replace(/\//g, ''));
|
|
|
|
} else if (urlSplitted.length == 2) { // URL has format: hostname + :port
|
|
|
|
this.hostname = this.urlOpenViduServer.split(':')[0].replace(/\//g, '');
|
|
|
|
this.port = parseInt(this.urlOpenViduServer.split(':')[1].replace(/\//g, ''));
|
|
|
|
} else {
|
|
|
|
console.error("URL format incorrect: it must contain hostname and port (current value: '" + this.urlOpenViduServer + "')");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|