Skip to content

Commit

Permalink
save original or clients socket io if exists and save socket io from …
Browse files Browse the repository at this point in the history
…cdn to vc_io
  • Loading branch information
Ostap Gadzeman committed Jan 7, 2025
1 parent 0841e72 commit f323ee4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class EventsSDK {
}

async _onLoginResponse(data) {
if (data.Client && !window.io) {
if (data.Client) {
await loadExternalScript('https://loginapi.voicenter.co.il/monitorAPI/GetSocketClient?v=2.4.0')
}
if (data.URL) {
Expand Down Expand Up @@ -299,7 +299,7 @@ class EventsSDK {
token: this.token
}
}
this.socket = window.io(url, options)
this.socket = window.vc_io(url, options)



Expand Down Expand Up @@ -409,7 +409,7 @@ class EventsSDK {
}

_onEvent(packet) {
if (!packet.data) {
if (!packet.data) {
return;
}
let evt = this._parsePacket(packet);
Expand Down
32 changes: 28 additions & 4 deletions src/utils/loadExternalScript.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
export function loadExternalScript(url) {
return new Promise((resolve) => {
return new Promise(async (resolve, reject) => {
let originalIo

if (typeof window !== 'undefined') {
if (typeof window.io !== 'undefined') {
originalIo = window.io;
}
}

const script = document.createElement('script');

script.src = url;

script.onload = () => {
resolve();
if (typeof window !== 'undefined') {
if (typeof window.io !== 'undefined') {
window.vc_io = window.io;

if (originalIo) {
window.io = originalIo;
}

resolve();
} else {
reject(new Error('socket.io-client was not loaded correctly.'));
}
}
};

script.onerror = () => {
reject();
reject(new Error('Failed to load the script.'));
};
document.body.append(script);

document.body.appendChild(script);
});
}

0 comments on commit f323ee4

Please sign in to comment.