import { TokenOptions } from './TokenOptions'; import { OpenViduRole } from './OpenViduRole'; import { SessionProperties } from './SessionProperties'; declare const Buffer; declare const require; let https = require('https'); export class Session { private static readonly API_SESSIONS: string = '/api/sessions'; private static readonly API_TOKENS: string = '/api/tokens'; private sessionId: string = ""; private properties: SessionProperties; constructor(private hostname: string, private port: number, private basicAuth: string, properties?: SessionProperties) { if (properties == null) { this.properties = new SessionProperties.Builder().build(); } else { this.properties = properties; } } public getSessionId(): Promise { return new Promise((resolve, reject) => { if (this.sessionId) { resolve(this.sessionId); } let requestBody = JSON.stringify({ 'recordingLayout': this.properties.recordingLayout(), 'recordingMode': this.properties.recordingMode(), 'mediaMode': this.properties.mediaMode() }); let options = { hostname: this.hostname, port: this.port, path: Session.API_SESSIONS, method: 'POST', headers: { 'Authorization': this.basicAuth, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(requestBody) } } const req = https.request(options, (res) => { let body = ''; res.on('data', (d) => { // Continuously update stream with data body += d; }); res.on('end', () => { if (res.statusCode === 200) { // SUCCESS response from openvidu-server. Resolve sessionId let parsed = JSON.parse(body); this.sessionId = parsed.id; resolve(parsed.id); } else { // ERROR response from openvidu-server. Resolve HTTP status reject(new Error(res.statusCode)); } }); }); req.on('error', (e) => { reject(e); }); req.write(requestBody); req.end(); }); } public generateToken(tokenOptions?: TokenOptions): Promise { return new Promise((resolve, reject) => { let requestBody; if (!!tokenOptions) { requestBody = JSON.stringify({ 'session': this.sessionId, 'role': tokenOptions.getRole(), 'data': tokenOptions.getData() }); } else { requestBody = JSON.stringify({ 'session': this.sessionId, 'role': OpenViduRole.PUBLISHER, 'data': '' }); } let options = { hostname: this.hostname, port: this.port, path: Session.API_TOKENS, method: 'POST', headers: { 'Authorization': this.basicAuth, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(requestBody) } }; const req = https.request(options, (res) => { let body = ''; res.on('data', (d) => { // Continuously update stream with data body += d; }); res.on('end', () => { if (res.statusCode === 200) { // SUCCESS response from openvidu-server. Resolve token let parsed = JSON.parse(body); resolve(parsed.id); } else { // ERROR response from openvidu-server. Resolve HTTP status reject(new Error(res.statusCode)); } }); }); req.on('error', (e) => { reject(e); }); req.write(requestBody); req.end(); }); } public getProperties(): SessionProperties { return this.properties; } }