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']) { if (platform['isInternetExplorer']) {
console.info("Detected IE Explorer " + platform.version); console.info("Detected IE Explorer " + platform.version);
this.importIEAdapterJS(); this.importIEAdapterJS();
this.importURLLibrary();
this.setGlobalIEFunctions(); 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 { private setGlobalIEFunctions(): void {
// FIX: the IE plugin seems to require the handler functions to be globally accessible. Store the functions with unique streamId // 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> { 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()) { if (this.openvidu.checkSystemRequirements()) {
// Early configuration to deactivate automatic subscription to streams // Early configuration to deactivate automatic subscription to streams
@ -1143,19 +1143,36 @@ export class Session implements EventDispatcher {
}); });
} }
private async processToken(token: string): Promise<void> { private processToken(token: string): void {
return new Promise<void>((resolve, reject) => { 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 params = token.split('?');
const url = new URL(token); const queryParams = decodeURI(params[1])
this.sessionId = <string>url.searchParams.get('sessionId'); .split('&')
const secret = url.searchParams.get('secret'); .map(param => param.split('='))
const recorder = url.searchParams.get('recorder'); .reduce((values, [key, value]) => {
const turnUsername = url.searchParams.get('turnUsername'); values[key] = value
const turnCredential = url.searchParams.get('turnCredential'); return values
const role = url.searchParams.get('role'); }, {});
const webrtcStatsInterval = url.searchParams.get('webrtcStatsInterval');
const openviduServerVersion = url.searchParams.get('version'); 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) { if (!!secret) {
this.openvidu.secret = secret; this.openvidu.secret = secret;
@ -1191,26 +1208,9 @@ export class Session implements EventDispatcher {
this.openvidu.wsUri = 'wss://' + url.host + '/openvidu'; this.openvidu.wsUri = 'wss://' + url.host + '/openvidu';
this.openvidu.httpUri = 'https://' + url.host; 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 { } else {
// Wait for the script tag to be loaded console.error('Token "' + token + '" is not valid')
this.openvidu.ee.once(scriptId, () => {
processTokenAux(token);
});
} }
} else {
processTokenAux(token);
}
});
} }
} }