openvidu/openvidu-node-client/src/Session.ts

127 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-06-10 01:44:31 +02:00
import { TokenOptions } from './TokenOptions';
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) {
callback(this.sessionId);
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();
}
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 + "')");
}
}
}