openvidu-browser: remove URL API usage

pull/255/head
pabloFuente 2019-05-20 14:11:14 +02:00
parent 227eefd99b
commit 06d455fc43
2 changed files with 68 additions and 89 deletions

View File

@ -111,7 +111,6 @@ export class OpenVidu {
if (platform['isInternetExplorer']) {
console.info("Detected IE Explorer " + platform.version);
this.importIEAdapterJS();
this.importURLLibrary();
this.setGlobalIEFunctions();
}
@ -784,26 +783,6 @@ export class OpenVidu {
}
}
private importURLLibrary(): void {
const moduleSpecifier = 'https://cdn.jsdelivr.net/npm/url-polyfill@1.1.5/url-polyfill.min.js';
const scriptId = 'url-script-element';
var script = document.createElement('script');
script.async = false;
script.id = scriptId;
script.src = moduleSpecifier;
script.onload = () => {
// URL feature is now available
this.ee.emitEvent(scriptId, []);
document.getElementById(scriptId)!.classList.add(scriptId);
};
var ref = document.querySelector('script');
// Insert the new script node before the first reference node
if (ref && ref.parentNode) {
ref.parentNode.insertBefore(script, ref);
console.info("URL polyfill imported");
}
}
private setGlobalIEFunctions(): void {
// FIX: the IE plugin seems to require the handler functions to be globally accessible. Store the functions with unique streamId

View File

@ -143,9 +143,9 @@ export class Session implements EventDispatcher {
*
*/
connect(token: string, metadata?: any): Promise<any> {
return new Promise(async (resolve, reject) => {
return new Promise((resolve, reject) => {
await this.processToken(token);
this.processToken(token);
if (this.openvidu.checkSystemRequirements()) {
// Early configuration to deactivate automatic subscription to streams
@ -1143,19 +1143,36 @@ export class Session implements EventDispatcher {
});
}
private async processToken(token: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
private processToken(token: string): void {
const match = token.match(/^(wss?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
if (!!match) {
const url = {
protocol: match[1],
host: match[2],
hostname: match[3],
port: match[4],
pathname: match[5],
search: match[6],
hash: match[7]
};
const processTokenAux: Function = token => {
const url = new URL(token);
this.sessionId = <string>url.searchParams.get('sessionId');
const secret = url.searchParams.get('secret');
const recorder = url.searchParams.get('recorder');
const turnUsername = url.searchParams.get('turnUsername');
const turnCredential = url.searchParams.get('turnCredential');
const role = url.searchParams.get('role');
const webrtcStatsInterval = url.searchParams.get('webrtcStatsInterval');
const openviduServerVersion = url.searchParams.get('version');
const params = token.split('?');
const queryParams = decodeURI(params[1])
.split('&')
.map(param => param.split('='))
.reduce((values, [key, value]) => {
values[key] = value
return values
}, {});
this.sessionId = <string>queryParams['sessionId'];
const secret = queryParams['secret'];
const recorder = queryParams['recorder'];
const turnUsername = queryParams['turnUsername'];
const turnCredential = queryParams['turnCredential'];
const role = queryParams['role'];
const webrtcStatsInterval = queryParams['webrtcStatsInterval'];
const openviduServerVersion = queryParams['version'];
if (!!secret) {
this.openvidu.secret = secret;
@ -1191,26 +1208,9 @@ export class Session implements EventDispatcher {
this.openvidu.wsUri = 'wss://' + url.host + '/openvidu';
this.openvidu.httpUri = 'https://' + url.host;
resolve();
};
if (platform['isInternetExplorer']) {
// Wait for URL polyfill to be available
const scriptId = 'url-script-element';
const script = document.getElementById(scriptId);
if (script!.classList.contains(scriptId)) {
// URL polyfill is already available
processTokenAux(token);
} else {
// Wait for the script tag to be loaded
this.openvidu.ee.once(scriptId, () => {
processTokenAux(token);
});
console.error('Token "' + token + '" is not valid')
}
} else {
processTokenAux(token);
}
});
}
}