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

146 lines
4.6 KiB
TypeScript
Raw Normal View History

2017-06-10 01:44:31 +02:00
import { TokenOptions } from './TokenOptions';
import { OpenViduRole } from './OpenViduRole';
import { SessionProperties } from './SessionProperties';
2017-06-10 01:44:31 +02:00
declare const Buffer;
declare const require;
let https = require('https');
2017-06-10 01:44:31 +02:00
export class Session {
private sessionIdURL: string = '/api/sessions';
private tokenURL: string = '/api/tokens';
private sessionId: string = "";
private properties: SessionProperties;
2017-06-10 01:44:31 +02:00
private hostname: string;
private port: number;
constructor(private urlOpenViduServer: string, private secret: string, properties?: SessionProperties) {
if (properties == null) {
this.properties = new SessionProperties.Builder().build();
} else {
this.properties = properties;
}
2017-06-10 01:44:31 +02:00
this.setHostnameAndPort();
}
public getSessionId(callback: Function) {
if (this.sessionId) {
callback(this.sessionId);
return;
2017-06-10 01:44:31 +02:00
}
let requestBody = JSON.stringify({
'archiveLayout': this.properties.archiveLayout(),
'archiveMode': this.properties.archiveMode(),
'mediaMode': this.properties.mediaMode()
});
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(),
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestBody)
2017-06-10 01:44:31 +02:00
}
}
const req = https.request(options, (res) => {
let body = '';
2017-06-10 01:44:31 +02:00
res.on('data', (d) => {
// Continuously update stream with data
body += d;
});
res.on('end', () => {
// Data reception is done
let parsed = JSON.parse(body);
2017-06-10 01:44:31 +02:00
this.sessionId = parsed.id;
callback(parsed.id);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(requestBody);
2017-06-10 01:44:31 +02:00
req.end();
}
public generateToken(callback: Function);
public generateToken(tokenOptions: TokenOptions, callback: Function);
public generateToken(tokenOptions: any, callback?: any) {
let 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;
}
let options = {
2017-06-10 01:44:31 +02:00
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) => {
let body = '';
2017-06-10 01:44:31 +02:00
res.on('data', (d) => {
// Continuously update stream with data
body += d;
});
res.on('end', () => {
// Data reception is done
let parsed = JSON.parse(body);
2017-06-10 01:44:31 +02:00
callback(parsed.id);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(requestBody);
req.end();
}
public getProperties(): SessionProperties {
return this.properties;
}
2017-06-10 01:44:31 +02:00
private getBasicAuth() {
return 'Basic ' + (new Buffer('OPENVIDUAPP:' + this.secret).toString('base64'));
}
private setHostnameAndPort() {
let urlSplitted = this.urlOpenViduServer.split(':');
2017-06-10 01:44:31 +02:00
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 + "')");
}
}
}