2018-04-18 14:29:07 +02:00
|
|
|
/*
|
2019-01-21 21:32:17 +01:00
|
|
|
* (C) Copyright 2017-2019 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-09-19 15:36:26 +02:00
|
|
|
import axios from 'axios';
|
2018-07-22 22:13:45 +02:00
|
|
|
import { Connection } from './Connection';
|
2018-04-23 11:06:16 +02:00
|
|
|
import { MediaMode } from './MediaMode';
|
2018-07-22 22:13:45 +02:00
|
|
|
import { OpenVidu } from './OpenVidu';
|
2017-06-19 10:16:14 +02:00
|
|
|
import { OpenViduRole } from './OpenViduRole';
|
2018-07-22 22:13:45 +02:00
|
|
|
import { Publisher } from './Publisher';
|
2019-01-29 12:31:38 +01:00
|
|
|
import { Recording } from './Recording';
|
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
|
|
|
|
|
|
|
|
|
|
|
export class Session {
|
|
|
|
|
2018-07-22 22:13:45 +02:00
|
|
|
/**
|
|
|
|
* Unique identifier of the Session
|
|
|
|
*/
|
2018-04-24 15:42:23 +02:00
|
|
|
sessionId: string;
|
2018-07-22 22:13:45 +02:00
|
|
|
|
2018-09-06 12:02:07 +02:00
|
|
|
/**
|
|
|
|
* Timestamp when this session was created, in UTC milliseconds (ms since Jan 1, 1970, 00:00:00 UTC)
|
|
|
|
*/
|
|
|
|
createdAt: number;
|
|
|
|
|
2018-07-22 22:13:45 +02:00
|
|
|
/**
|
|
|
|
* Properties defining the session
|
|
|
|
*/
|
2018-04-24 15:42:23 +02:00
|
|
|
properties: SessionProperties;
|
2017-06-10 01:44:31 +02:00
|
|
|
|
2018-07-22 22:13:45 +02:00
|
|
|
/**
|
|
|
|
* Array of active connections to the session. This property always initialize as an empty array and
|
|
|
|
* **will remain unchanged since the last time method [[Session.fetch]] was called**. Exceptions to this rule are:
|
|
|
|
*
|
|
|
|
* - Calling [[Session.forceUnpublish]] also automatically updates each affected Connection status
|
|
|
|
* - Calling [[Session.forceDisconnect]] automatically updates each affected Connection status
|
|
|
|
*
|
|
|
|
* To get the array of active connections with their current actual value, you must call [[Session.fetch]] before consulting
|
|
|
|
* property [[activeConnections]]
|
|
|
|
*/
|
|
|
|
activeConnections: Connection[] = [];
|
2018-06-04 14:14:17 +02:00
|
|
|
|
2018-07-22 22:13:45 +02:00
|
|
|
/**
|
|
|
|
* Whether the session is being recorded or not
|
|
|
|
*/
|
|
|
|
recording = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
2019-05-22 17:01:30 +02:00
|
|
|
constructor(private ov: OpenVidu, propertiesOrJson?) {
|
2018-07-22 22:13:45 +02:00
|
|
|
if (!!propertiesOrJson) {
|
|
|
|
// Defined parameter
|
|
|
|
if (!!propertiesOrJson.sessionId) {
|
|
|
|
// Parameter is a JSON representation of Session ('sessionId' property always defined)
|
|
|
|
this.resetSessionWithJson(propertiesOrJson);
|
|
|
|
} else {
|
|
|
|
// Parameter is a SessionProperties object
|
|
|
|
this.properties = propertiesOrJson;
|
|
|
|
}
|
2018-01-27 19:39:49 +01:00
|
|
|
} else {
|
2018-07-22 22:13:45 +02:00
|
|
|
// Empty parameter
|
|
|
|
this.properties = {};
|
2018-01-27 19:39:49 +01:00
|
|
|
}
|
2018-07-22 22:13:45 +02:00
|
|
|
this.properties.mediaMode = !!this.properties.mediaMode ? this.properties.mediaMode : MediaMode.ROUTED;
|
|
|
|
this.properties.recordingMode = !!this.properties.recordingMode ? this.properties.recordingMode : RecordingMode.MANUAL;
|
2019-01-29 12:31:38 +01:00
|
|
|
this.properties.defaultOutputMode = !!this.properties.defaultOutputMode ? this.properties.defaultOutputMode : Recording.OutputMode.COMPOSED;
|
2018-07-22 22:13:45 +02:00
|
|
|
this.properties.defaultRecordingLayout = !!this.properties.defaultRecordingLayout ? this.properties.defaultRecordingLayout : RecordingLayout.BEST_FIT;
|
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,
|
2018-07-27 11:54:34 +02:00
|
|
|
data: (!!tokenOptions && !!tokenOptions.data) ? tokenOptions.data : '',
|
2018-08-01 15:10:46 +02:00
|
|
|
kurentoOptions: (!!tokenOptions && !!tokenOptions.kurentoOptions) ? tokenOptions.kurentoOptions : {},
|
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(
|
2019-05-22 17:01:30 +02:00
|
|
|
'https://' + this.ov.hostname + ':' + this.ov.port + OpenVidu.API_TOKENS,
|
2018-06-04 14:14:17 +02:00
|
|
|
data,
|
|
|
|
{
|
|
|
|
headers: {
|
2019-05-22 17:01:30 +02:00
|
|
|
'Authorization': this.ov.basicAuth,
|
2018-06-04 14:14:17 +02:00
|
|
|
'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);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.request));
|
2018-06-04 14:14:17 +02:00
|
|
|
} 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);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.message));
|
2018-07-13 12:56:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gracefully closes the Session: unpublishes all streams and evicts every participant
|
|
|
|
*
|
2018-07-22 22:13:45 +02:00
|
|
|
* @returns A Promise that is resolved if the session has been closed successfully and rejected with an Error object if not
|
2018-07-13 12:56:27 +02:00
|
|
|
*/
|
2018-07-22 22:13:45 +02:00
|
|
|
public close(): Promise<any> {
|
|
|
|
return new Promise<any>((resolve, reject) => {
|
|
|
|
axios.delete(
|
2019-05-22 17:01:30 +02:00
|
|
|
'https://' + this.ov.hostname + ':' + this.ov.port + OpenVidu.API_SESSIONS + '/' + this.sessionId,
|
2018-07-22 22:13:45 +02:00
|
|
|
{
|
|
|
|
headers: {
|
2019-05-22 17:01:30 +02:00
|
|
|
'Authorization': this.ov.basicAuth,
|
2018-07-22 22:13:45 +02:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
if (res.status === 204) {
|
|
|
|
// SUCCESS response from openvidu-server
|
2019-05-22 17:01:30 +02:00
|
|
|
const indexToRemove: number = this.ov.activeSessions.findIndex(s => s.sessionId === this.sessionId);
|
|
|
|
this.ov.activeSessions.splice(indexToRemove, 1);
|
2018-07-22 22:13:45 +02:00
|
|
|
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);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.request));
|
2018-07-22 22:13:45 +02:00
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
console.error('Error', error.message);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.message));
|
2018-07-22 22:13:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates every property of the Session with the current status it has in OpenVidu Server. This is especially useful for accessing the list of active
|
2018-09-28 10:29:14 +02:00
|
|
|
* connections of the Session ([[Session.activeConnections]]) and use those values to call [[Session.forceDisconnect]] or [[Session.forceUnpublish]].
|
|
|
|
*
|
|
|
|
* To update every Session object owned by OpenVidu object, call [[OpenVidu.fetch]]
|
2018-07-22 22:13:45 +02:00
|
|
|
*
|
|
|
|
* @returns A promise resolved to true if the Session status has changed with respect to the server, or to false if not.
|
|
|
|
* This applies to any property or sub-property of the Session object
|
|
|
|
*/
|
|
|
|
public fetch(): Promise<boolean> {
|
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
2019-05-31 14:38:20 +02:00
|
|
|
const beforeJSON: string = JSON.stringify(this, this.removeCircularOpenViduReference);
|
2018-07-22 22:13:45 +02:00
|
|
|
axios.get(
|
2019-05-22 17:01:30 +02:00
|
|
|
'https://' + this.ov.hostname + ':' + this.ov.port + OpenVidu.API_SESSIONS + '/' + this.sessionId,
|
2018-07-22 22:13:45 +02:00
|
|
|
{
|
|
|
|
headers: {
|
2019-05-22 17:01:30 +02:00
|
|
|
'Authorization': this.ov.basicAuth,
|
2018-07-22 22:13:45 +02:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
if (res.status === 200) {
|
|
|
|
// SUCCESS response from openvidu-server
|
|
|
|
this.resetSessionWithJson(res.data);
|
2019-05-31 14:38:20 +02:00
|
|
|
const afterJSON: string = JSON.stringify(this, this.removeCircularOpenViduReference);
|
2018-07-22 22:13:45 +02:00
|
|
|
const hasChanged: boolean = !(beforeJSON === afterJSON);
|
|
|
|
console.log("Session info fetched for session '" + this.sessionId + "'. Any change: " + hasChanged);
|
|
|
|
resolve(hasChanged);
|
|
|
|
} 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);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.request));
|
2018-07-22 22:13:45 +02:00
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
console.error('Error', error.message);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.message));
|
2018-07-22 22:13:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces the user with Connection `connectionId` to leave the session. OpenVidu Browser will trigger the proper events on the client-side
|
|
|
|
* (`streamDestroyed`, `connectionDestroyed`, `sessionDisconnected`) with reason set to `"forceDisconnectByServer"`
|
|
|
|
*
|
|
|
|
* You can get `connection` parameter from [[Session.activeConnections]] array ([[Connection.connectionId]] for getting each `connectionId` property).
|
|
|
|
* Remember to call [[Session.fetch]] before to fetch the current actual properties of the Session from OpenVidu Server
|
|
|
|
*
|
|
|
|
* @returns A Promise that is resolved if the user was successfully disconnected and rejected with an Error object if not
|
|
|
|
*/
|
|
|
|
public forceDisconnect(connection: string | Connection): Promise<any> {
|
|
|
|
return new Promise<any>((resolve, reject) => {
|
|
|
|
const connectionId: string = typeof connection === 'string' ? connection : (<Connection>connection).connectionId;
|
|
|
|
axios.delete(
|
2019-05-22 17:01:30 +02:00
|
|
|
'https://' + this.ov.hostname + ':' + this.ov.port + OpenVidu.API_SESSIONS + '/' + this.sessionId + '/connection/' + connectionId,
|
2018-07-22 22:13:45 +02:00
|
|
|
{
|
|
|
|
headers: {
|
2019-05-22 17:01:30 +02:00
|
|
|
'Authorization': this.ov.basicAuth,
|
2018-07-22 22:13:45 +02:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.status === 204) {
|
|
|
|
// SUCCESS response from openvidu-server
|
|
|
|
// Remove connection from activeConnections array
|
|
|
|
let connectionClosed;
|
|
|
|
this.activeConnections = this.activeConnections.filter(con => {
|
|
|
|
if (con.connectionId !== connectionId) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
connectionClosed = con;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Remove every Publisher of the closed connection from every subscriber list of other connections
|
|
|
|
if (!!connectionClosed) {
|
|
|
|
connectionClosed.publishers.forEach(publisher => {
|
|
|
|
this.activeConnections.forEach(con => {
|
2018-09-24 11:04:54 +02:00
|
|
|
con.subscribers = con.subscribers.filter(subscriber => {
|
|
|
|
// tslint:disable:no-string-literal
|
|
|
|
if (!!subscriber['streamId']) {
|
|
|
|
// Subscriber with advanced webRtc configuration properties
|
|
|
|
return (subscriber['streamId'] !== publisher.streamId);
|
|
|
|
// tslint:enable:no-string-literal
|
|
|
|
} else {
|
|
|
|
// Regular string subscribers
|
|
|
|
return subscriber !== publisher.streamId;
|
|
|
|
}
|
|
|
|
});
|
2018-07-22 22:13:45 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.warn("The closed connection wasn't fetched in OpenVidu Java Client. No changes in the collection of active connections of the Session");
|
|
|
|
}
|
|
|
|
console.log("Connection '" + connectionId + "' closed");
|
|
|
|
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);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.request));
|
2018-07-22 22:13:45 +02:00
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
console.error('Error', error.message);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.message));
|
2018-07-22 22:13:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces some user to unpublish a Stream (identified by its `streamId` or the corresponding [[Publisher]] object owning it).
|
|
|
|
* OpenVidu Browser will trigger the proper events on the client-side (`streamDestroyed`) with reason set to `"forceUnpublishByServer"`.
|
|
|
|
*
|
|
|
|
* You can get `publisher` parameter from [[Connection.publishers]] array ([[Publisher.streamId]] for getting each `streamId` property).
|
|
|
|
* Remember to call [[Session.fetch]] before to fetch the current actual properties of the Session from OpenVidu Server
|
|
|
|
*
|
|
|
|
* @returns A Promise that is resolved if the stream was successfully unpublished and rejected with an Error object if not
|
|
|
|
*/
|
|
|
|
public forceUnpublish(publisher: string | Publisher): Promise<any> {
|
|
|
|
return new Promise<any>((resolve, reject) => {
|
|
|
|
const streamId: string = typeof publisher === 'string' ? publisher : (<Publisher>publisher).streamId;
|
2018-07-13 12:56:27 +02:00
|
|
|
axios.delete(
|
2019-05-22 17:01:30 +02:00
|
|
|
'https://' + this.ov.hostname + ':' + this.ov.port + OpenVidu.API_SESSIONS + '/' + this.sessionId + '/stream/' + streamId,
|
2018-07-13 12:56:27 +02:00
|
|
|
{
|
|
|
|
headers: {
|
2019-05-22 17:01:30 +02:00
|
|
|
'Authorization': this.ov.basicAuth,
|
2018-07-13 12:56:27 +02:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
if (res.status === 204) {
|
|
|
|
// SUCCESS response from openvidu-server
|
2018-07-22 22:13:45 +02:00
|
|
|
this.activeConnections.forEach(connection => {
|
|
|
|
// Try to remove the Publisher from the Connection publishers collection
|
|
|
|
connection.publishers = connection.publishers.filter(pub => pub.streamId !== streamId);
|
|
|
|
// Try to remove the Publisher from the Connection subscribers collection
|
2018-09-24 11:04:54 +02:00
|
|
|
if (!!connection.subscribers && connection.subscribers.length > 0) {
|
|
|
|
// tslint:disable:no-string-literal
|
|
|
|
if (!!connection.subscribers[0]['streamId']) {
|
|
|
|
// Subscriber with advanced webRtc configuration properties
|
|
|
|
connection.subscribers = connection.subscribers.filter(sub => sub['streamId'] !== streamId);
|
|
|
|
// tslint:enable:no-string-literal
|
|
|
|
} else {
|
|
|
|
// Regular string subscribers
|
|
|
|
connection.subscribers = connection.subscribers.filter(sub => sub !== streamId);
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 22:13:45 +02:00
|
|
|
});
|
|
|
|
console.log("Stream '" + streamId + "' unpublished");
|
2018-07-13 12:56:27 +02:00
|
|
|
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);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.request));
|
2018-07-13 12:56:27 +02:00
|
|
|
} 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-09-03 09:54:21 +02:00
|
|
|
reject(new 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,
|
2019-01-29 12:31:38 +01:00
|
|
|
defaultOutputMode: !!this.properties.defaultOutputMode ? this.properties.defaultOutputMode : Recording.OutputMode.COMPOSED,
|
2018-04-25 17:50:55 +02:00
|
|
|
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(
|
2019-05-22 17:01:30 +02:00
|
|
|
'https://' + this.ov.hostname + ':' + this.ov.port + OpenVidu.API_SESSIONS,
|
2018-06-04 14:14:17 +02:00
|
|
|
data,
|
|
|
|
{
|
|
|
|
headers: {
|
2019-05-22 17:01:30 +02:00
|
|
|
'Authorization': this.ov.basicAuth,
|
2018-06-04 14:14:17 +02:00
|
|
|
'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-09-06 12:02:07 +02:00
|
|
|
this.createdAt = res.data.createdAt;
|
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);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.request));
|
2018-06-04 14:14:17 +02:00
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
console.error('Error', error.message);
|
2018-09-03 09:54:21 +02:00
|
|
|
reject(new Error(error.message));
|
2018-04-18 10:56:28 +02:00
|
|
|
}
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-22 22:13:45 +02:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
public resetSessionWithJson(json): Session {
|
|
|
|
this.sessionId = json.sessionId;
|
2018-09-19 15:36:26 +02:00
|
|
|
this.createdAt = json.createdAt;
|
2018-07-22 22:13:45 +02:00
|
|
|
this.recording = json.recording;
|
|
|
|
let customSessionId: string;
|
|
|
|
let defaultCustomLayout: string;
|
|
|
|
if (!!this.properties) {
|
|
|
|
customSessionId = this.properties.customSessionId;
|
|
|
|
defaultCustomLayout = !!json.defaultCustomLayout ? json.defaultCustomLayout : this.properties.defaultCustomLayout;
|
|
|
|
}
|
|
|
|
this.properties = {
|
|
|
|
mediaMode: json.mediaMode,
|
|
|
|
recordingMode: json.recordingMode,
|
2019-01-29 12:31:38 +01:00
|
|
|
defaultOutputMode: json.defaultOutputMode,
|
2018-07-22 22:13:45 +02:00
|
|
|
defaultRecordingLayout: json.defaultRecordingLayout
|
|
|
|
};
|
|
|
|
if (!!customSessionId) {
|
|
|
|
this.properties.customSessionId = customSessionId;
|
2018-09-20 18:18:32 +02:00
|
|
|
} else if (!!json.customSessionId) {
|
|
|
|
this.properties.customSessionId = json.customSessionId;
|
2018-07-22 22:13:45 +02:00
|
|
|
}
|
|
|
|
if (!!defaultCustomLayout) {
|
|
|
|
this.properties.defaultCustomLayout = defaultCustomLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.activeConnections = [];
|
|
|
|
json.connections.content.forEach(connection => {
|
|
|
|
const publishers: Publisher[] = [];
|
|
|
|
connection.publishers.forEach(publisher => {
|
|
|
|
publishers.push(new Publisher(publisher));
|
|
|
|
});
|
|
|
|
const subscribers: string[] = [];
|
|
|
|
connection.subscribers.forEach(subscriber => {
|
|
|
|
subscribers.push(subscriber.streamId);
|
|
|
|
});
|
2018-09-06 12:02:07 +02:00
|
|
|
this.activeConnections.push(
|
|
|
|
new Connection(
|
|
|
|
connection.connectionId,
|
|
|
|
connection.createdAt,
|
|
|
|
connection.role,
|
|
|
|
connection.token,
|
|
|
|
connection.location,
|
|
|
|
connection.platform,
|
|
|
|
connection.serverData,
|
|
|
|
connection.clientData,
|
|
|
|
publishers,
|
|
|
|
subscribers));
|
2018-07-22 22:13:45 +02:00
|
|
|
});
|
2018-09-20 12:24:07 +02:00
|
|
|
// Order connections by time of creation
|
|
|
|
this.activeConnections.sort((c1, c2) => (c1.createdAt > c2.createdAt) ? 1 : ((c2.createdAt > c1.createdAt) ? -1 : 0));
|
2018-07-22 22:13:45 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-19 15:36:26 +02:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
equalTo(other: Session): boolean {
|
|
|
|
let equals: boolean = (
|
|
|
|
this.sessionId === other.sessionId &&
|
|
|
|
this.createdAt === other.createdAt &&
|
|
|
|
this.recording === other.recording &&
|
|
|
|
this.activeConnections.length === other.activeConnections.length &&
|
|
|
|
JSON.stringify(this.properties) === JSON.stringify(other.properties)
|
|
|
|
);
|
|
|
|
if (equals) {
|
|
|
|
let i = 0;
|
|
|
|
while (equals && i < this.activeConnections.length) {
|
|
|
|
equals = this.activeConnections[i].equalTo(other.activeConnections[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return equals;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 14:38:20 +02:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
private removeCircularOpenViduReference(key: string, value: any) {
|
|
|
|
if (key === 'ov' && value instanceof OpenVidu) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|