openvidu-node-client: update fetchAllWebRtc with individual session info

pull/154/head
pabloFuente 2018-10-09 16:29:28 +02:00
parent d031d0e467
commit 5af65bb5b8
1 changed files with 19 additions and 8 deletions

View File

@ -23,6 +23,11 @@ import { RecordingProperties } from './RecordingProperties';
import { Session } from './Session'; import { Session } from './Session';
import { SessionProperties } from './SessionProperties'; import { SessionProperties } from './SessionProperties';
/**
* @hidden
*/
interface ObjMap<T> { [s: string]: T; }
export class OpenVidu { export class OpenVidu {
@ -476,8 +481,9 @@ export class OpenVidu {
/** /**
* @hidden * @hidden
* @returns A map paring every existing sessionId with true or false depending on whether it has changed or not
*/ */
fetchWebRtc(): Promise<boolean> { fetchWebRtc(): Promise<any> {
// tslint:disable:no-string-literal // tslint:disable:no-string-literal
const addWebRtcStatsToConnections = (connection: Connection, connectionsExtendedInfo: any) => { const addWebRtcStatsToConnections = (connection: Connection, connectionsExtendedInfo: any) => {
@ -561,7 +567,7 @@ export class OpenVidu {
}; };
}; };
return new Promise<boolean>((resolve, reject) => { return new Promise<{ changes: boolean, sessionChanges: ObjMap<boolean> }>((resolve, reject) => {
axios.get( axios.get(
'https://' + OpenVidu.hostname + ':' + OpenVidu.port + OpenVidu.API_SESSIONS + '?webRtcStats=true', 'https://' + OpenVidu.hostname + ':' + OpenVidu.port + OpenVidu.API_SESSIONS + '?webRtcStats=true',
{ {
@ -575,8 +581,10 @@ export class OpenVidu {
// Array to store fetched sessionIds and later remove closed sessions // Array to store fetched sessionIds and later remove closed sessions
const fetchedSessionIds: string[] = []; const fetchedSessionIds: string[] = [];
// Boolean to store if any Session has changed // Global changes
let hasChanged = false; let globalChanges = false;
// Collection of sessionIds telling whether each one of them has changed or not
const sessionChanges: ObjMap<boolean> = {};
res.data.content.forEach(session => { res.data.content.forEach(session => {
fetchedSessionIds.push(session.sessionId); fetchedSessionIds.push(session.sessionId);
@ -609,7 +617,8 @@ export class OpenVidu {
this.activeSessions[sessionIndex] = storedSession; this.activeSessions[sessionIndex] = storedSession;
} }
console.log("Available session '" + storedSession.sessionId + "' info fetched. Any change: " + changed); console.log("Available session '" + storedSession.sessionId + "' info fetched. Any change: " + changed);
hasChanged = hasChanged || changed; sessionChanges[storedSession.sessionId] = changed;
globalChanges = globalChanges || changed;
} else { } else {
const newSession = new Session(session); const newSession = new Session(session);
newSession.activeConnections.forEach(connection => { newSession.activeConnections.forEach(connection => {
@ -617,7 +626,8 @@ export class OpenVidu {
}); });
this.activeSessions.push(newSession); this.activeSessions.push(newSession);
console.log("New session '" + session.sessionId + "' info fetched"); console.log("New session '" + session.sessionId + "' info fetched");
hasChanged = true; sessionChanges[session.sessionId] = true;
globalChanges = true;
} }
}); });
// Remove closed sessions from activeSessions array // Remove closed sessions from activeSessions array
@ -626,12 +636,13 @@ export class OpenVidu {
return true; return true;
} else { } else {
console.log("Removing closed session '" + session.sessionId + "'"); console.log("Removing closed session '" + session.sessionId + "'");
hasChanged = true; sessionChanges[session.sessionId] = true;
globalChanges = true;
return false; return false;
} }
}); });
console.log('Active sessions info fetched: ', fetchedSessionIds); console.log('Active sessions info fetched: ', fetchedSessionIds);
resolve(hasChanged); resolve({ changes: globalChanges, sessionChanges });
} else { } else {
// ERROR response from openvidu-server. Resolve HTTP status // ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.status.toString())); reject(new Error(res.status.toString()));