2018-04-18 14:29:07 +02:00
|
|
|
/*
|
2018-05-06 02:20:25 +02:00
|
|
|
* (C) Copyright 2017-2018 OpenVidu (https://openvidu.io/)
|
2018-04-18 14:29:07 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
import { MediaMode } from './MediaMode';
|
2017-06-19 10:16:14 +02:00
|
|
|
import { OpenViduRole } from './OpenViduRole';
|
2018-04-23 11:06:16 +02:00
|
|
|
import { RecordingLayout } from './RecordingLayout';
|
|
|
|
import { RecordingMode } from './RecordingMode';
|
2018-01-27 19:39:49 +01:00
|
|
|
import { SessionProperties } from './SessionProperties';
|
2018-04-23 11:06:16 +02:00
|
|
|
import { TokenOptions } from './TokenOptions';
|
2017-06-10 01:44:31 +02:00
|
|
|
|
2018-07-13 12:56:27 +02:00
|
|
|
import axios from 'axios';
|
2017-06-10 01:44:31 +02:00
|
|
|
|
|
|
|
export class Session {
|
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
private static readonly API_SESSIONS = '/api/sessions';
|
|
|
|
private static readonly API_TOKENS = '/api/tokens';
|
2018-03-14 18:48:29 +01:00
|
|
|
|
2018-04-24 15:42:23 +02:00
|
|
|
sessionId: string;
|
|
|
|
properties: SessionProperties;
|
2017-06-10 01:44:31 +02:00
|
|
|
|
2018-06-04 14:14:17 +02:00
|
|
|
private Buffer = require('buffer/').Buffer;
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
constructor(private hostname: string, private port: number, private basicAuth: string, properties?: SessionProperties) {
|
2018-04-24 15:42:23 +02:00
|
|
|
if (!properties) {
|
2018-04-23 11:06:16 +02:00
|
|
|
this.properties = {};
|
2018-01-27 19:39:49 +01:00
|
|
|
} else {
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 15:42:23 +02:00
|
|
|
/**
|
2018-04-25 17:50:55 +02:00
|
|
|
* Gets the unique identifier of the Session
|
|
|
|
*/
|
|
|
|
public getSessionId(): string {
|
|
|
|
return this.sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a new token associated to Session object
|
2018-04-24 15:42:23 +02:00
|
|
|
*
|
2018-04-25 17:50:55 +02:00
|
|
|
* @returns A Promise that is resolved to the _token_ if success and rejected with an Error object if not
|
2018-04-24 15:42:23 +02:00
|
|
|
*/
|
2018-04-25 17:50:55 +02:00
|
|
|
public generateToken(tokenOptions?: TokenOptions): Promise<string> {
|
2018-04-18 10:56:28 +02:00
|
|
|
return new Promise<string>((resolve, reject) => {
|
2017-06-10 01:44:31 +02:00
|
|
|
|
2018-06-04 14:14:17 +02:00
|
|
|
const data = JSON.stringify({
|
2018-04-25 17:50:55 +02:00
|
|
|
session: this.sessionId,
|
2018-05-08 14:49:42 +02:00
|
|
|
role: (!!tokenOptions && !!tokenOptions.role) ? tokenOptions.role : OpenViduRole.PUBLISHER,
|
|
|
|
data: (!!tokenOptions && !!tokenOptions.data) ? tokenOptions.data : ''
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
2018-04-18 10:56:28 +02:00
|
|
|
|
2018-06-04 14:14:17 +02:00
|
|
|
axios.post(
|
|
|
|
'https://' + this.hostname + ':' + this.port + Session.API_TOKENS,
|
|
|
|
data,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
2018-03-14 18:48:29 +01:00
|
|
|
}
|
2018-06-04 14:14:17 +02:00
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
if (res.status === 200) {
|
2018-04-25 17:50:55 +02:00
|
|
|
// SUCCESS response from openvidu-server. Resolve token
|
2018-06-04 14:14:17 +02:00
|
|
|
resolve(res.data.id);
|
2018-04-18 10:56:28 +02:00
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
2018-06-04 14:14:17 +02:00
|
|
|
reject(new Error(res.status.toString()));
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
if (error.response) {
|
|
|
|
// The request was made and the server responded with a status code (not 2xx)
|
|
|
|
reject(new Error(error.response.status.toString()));
|
|
|
|
} else if (error.request) {
|
|
|
|
// The request was made but no response was received
|
|
|
|
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
|
|
// http.ClientRequest in node.js
|
|
|
|
console.error(error.request);
|
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
2018-07-13 12:56:27 +02:00
|
|
|
console.error('Error', error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gracefully closes the Session: unpublishes all streams and evicts every participant
|
|
|
|
*
|
|
|
|
* @returns A Promise that is resolved to the if the session has been closed successfully and rejected with an Error object if not
|
|
|
|
*/
|
|
|
|
public close() {
|
|
|
|
return new Promise<string>((resolve, reject) => {
|
|
|
|
axios.delete(
|
|
|
|
'https://' + this.hostname + ':' + this.port + Session.API_SESSIONS + '/' + this.sessionId,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
if (res.status === 204) {
|
|
|
|
// SUCCESS response from openvidu-server
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.status.toString()));
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
if (error.response) {
|
|
|
|
// The request was made and the server responded with a status code (not 2xx)
|
|
|
|
reject(new Error(error.response.status.toString()));
|
|
|
|
} else if (error.request) {
|
|
|
|
// The request was made but no response was received
|
|
|
|
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
|
|
// http.ClientRequest in node.js
|
|
|
|
console.error(error.request);
|
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
2018-06-04 14:14:17 +02:00
|
|
|
console.error('Error', error.message);
|
2018-04-18 10:56:28 +02:00
|
|
|
}
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-24 15:42:23 +02:00
|
|
|
/**
|
2018-04-25 17:50:55 +02:00
|
|
|
* @hidden
|
2018-04-24 15:42:23 +02:00
|
|
|
*/
|
2018-04-25 17:50:55 +02:00
|
|
|
public getSessionIdHttp(): Promise<string> {
|
2018-04-18 10:56:28 +02:00
|
|
|
return new Promise<string>((resolve, reject) => {
|
|
|
|
|
2018-04-25 17:50:55 +02:00
|
|
|
if (!!this.sessionId) {
|
|
|
|
resolve(this.sessionId);
|
|
|
|
}
|
|
|
|
|
2018-06-04 14:14:17 +02:00
|
|
|
const data = JSON.stringify({
|
2018-04-25 17:50:55 +02:00
|
|
|
mediaMode: !!this.properties.mediaMode ? this.properties.mediaMode : MediaMode.ROUTED,
|
|
|
|
recordingMode: !!this.properties.recordingMode ? this.properties.recordingMode : RecordingMode.MANUAL,
|
|
|
|
defaultRecordingLayout: !!this.properties.defaultRecordingLayout ? this.properties.defaultRecordingLayout : RecordingLayout.BEST_FIT,
|
2018-05-03 10:58:26 +02:00
|
|
|
defaultCustomLayout: !!this.properties.defaultCustomLayout ? this.properties.defaultCustomLayout : '',
|
|
|
|
customSessionId: !!this.properties.customSessionId ? this.properties.customSessionId : ''
|
2018-04-23 11:06:16 +02:00
|
|
|
});
|
2018-04-18 10:56:28 +02:00
|
|
|
|
2018-06-04 14:14:17 +02:00
|
|
|
axios.post(
|
|
|
|
'https://' + this.hostname + ':' + this.port + Session.API_SESSIONS,
|
|
|
|
data,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
2018-03-14 18:48:29 +01:00
|
|
|
}
|
2018-06-04 14:14:17 +02:00
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
if (res.status === 200) {
|
|
|
|
// SUCCESS response from openvidu-server. Resolve token
|
|
|
|
this.sessionId = res.data.id;
|
2018-05-08 14:49:42 +02:00
|
|
|
resolve(this.sessionId);
|
2018-04-18 10:56:28 +02:00
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
2018-06-04 14:14:17 +02:00
|
|
|
reject(new Error(res.status.toString()));
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
if (error.response) {
|
|
|
|
// The request was made and the server responded with a status code (not 2xx)
|
|
|
|
if (error.response.status === 409) {
|
|
|
|
// 'customSessionId' already existed
|
|
|
|
this.sessionId = this.properties.customSessionId;
|
|
|
|
resolve(this.sessionId);
|
|
|
|
} else {
|
|
|
|
reject(new Error(error.response.status.toString()));
|
|
|
|
}
|
|
|
|
} else if (error.request) {
|
|
|
|
// The request was made but no response was received
|
|
|
|
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
|
|
// http.ClientRequest in node.js
|
|
|
|
console.error(error.request);
|
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
console.error('Error', error.message);
|
2018-04-18 10:56:28 +02:00
|
|
|
}
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|