2018-04-18 14:29:07 +02:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2017-2018 OpenVidu (http://openvidu.io/)
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-06-10 01:44:31 +02:00
|
|
|
import { TokenOptions } from './TokenOptions';
|
2017-06-19 10:16:14 +02:00
|
|
|
import { OpenViduRole } from './OpenViduRole';
|
2018-01-27 19:39:49 +01:00
|
|
|
import { SessionProperties } from './SessionProperties';
|
2017-06-10 01:44:31 +02:00
|
|
|
|
|
|
|
declare const Buffer;
|
|
|
|
declare const require;
|
|
|
|
|
2018-01-27 19:39:49 +01:00
|
|
|
let https = require('https');
|
2017-06-10 01:44:31 +02:00
|
|
|
|
|
|
|
export class Session {
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
private static readonly API_SESSIONS: string = '/api/sessions';
|
|
|
|
private static readonly API_TOKENS: string = '/api/tokens';
|
|
|
|
|
2017-06-10 01:44:31 +02:00
|
|
|
private sessionId: string = "";
|
2018-01-27 19:39:49 +01:00
|
|
|
private properties: SessionProperties;
|
2017-06-10 01:44:31 +02:00
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
constructor(private hostname: string, private port: number, private basicAuth: string, properties?: SessionProperties) {
|
2018-01-27 19:39:49 +01:00
|
|
|
if (properties == null) {
|
|
|
|
this.properties = new SessionProperties.Builder().build();
|
|
|
|
} else {
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 10:56:28 +02:00
|
|
|
public getSessionId(): Promise<string> {
|
|
|
|
return new Promise<string>((resolve, reject) => {
|
2017-06-10 01:44:31 +02:00
|
|
|
|
2018-04-18 10:56:28 +02:00
|
|
|
if (this.sessionId) {
|
|
|
|
resolve(this.sessionId);
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
|
|
|
|
let requestBody = JSON.stringify({
|
2018-04-20 12:04:56 +02:00
|
|
|
'defaultRecordingLayout': this.properties.defaultRecordingLayout(),
|
2018-04-18 10:56:28 +02:00
|
|
|
'recordingMode': this.properties.recordingMode(),
|
|
|
|
'mediaMode': this.properties.mediaMode()
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
2018-04-18 10:56:28 +02:00
|
|
|
|
|
|
|
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)
|
2018-03-14 18:48:29 +01:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
|
2018-04-18 10:56:28 +02:00
|
|
|
req.on('error', (e) => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
req.write(requestBody);
|
|
|
|
req.end();
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-18 10:56:28 +02:00
|
|
|
public generateToken(tokenOptions?: TokenOptions): Promise<string> {
|
|
|
|
return new Promise<string>((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': ''
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
|
|
|
|
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)
|
2018-03-14 18:48:29 +01:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
};
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
|
2018-04-18 10:56:28 +02:00
|
|
|
req.on('error', (e) => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
req.write(requestBody);
|
|
|
|
req.end();
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-27 19:39:49 +01:00
|
|
|
public getProperties(): SessionProperties {
|
2018-03-14 18:48:29 +01:00
|
|
|
return this.properties;
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|