diff --git a/packages/js/src/Modules/Verto/services/Connection.ts b/packages/js/src/Modules/Verto/services/Connection.ts index e9ad8507..32b1934b 100644 --- a/packages/js/src/Modules/Verto/services/Connection.ts +++ b/packages/js/src/Modules/Verto/services/Connection.ts @@ -10,6 +10,7 @@ import { } from '../util/helpers'; import { registerOnce, trigger } from './Handler'; import { GatewayStateType } from '../webrtc/constants'; +import { getReconnectToken, setReconnectToken } from '../util/reconnect'; let WebSocketClass: any = typeof WebSocket !== 'undefined' ? WebSocket : null; export const setWebSocket = (websocket: any): void => { @@ -70,6 +71,11 @@ export default class Connection { } connect() { + const reconnectToken = getReconnectToken(); + if (reconnectToken) { + this._host += `?voice_sdk_id=${reconnectToken}`; + } + this._wsClient = new WebSocketClass(this._host); this._wsClient.onopen = (event): boolean => trigger(SwEvent.SocketOpen, event, this.session.uuid); @@ -87,6 +93,10 @@ export default class Connection { this._handleStringResponse(msg); return; } + + if (msg.voice_sdk_id) { + setReconnectToken(msg.voice_sdk_id); + } this._unsetTimer(msg.id); logger.debug('RECV: \n', JSON.stringify(msg, null, 2), '\n'); diff --git a/packages/js/src/Modules/Verto/util/reconnect.ts b/packages/js/src/Modules/Verto/util/reconnect.ts new file mode 100644 index 00000000..bf253edd --- /dev/null +++ b/packages/js/src/Modules/Verto/util/reconnect.ts @@ -0,0 +1,18 @@ +const STORAGE_KEY = 'telnyx-voice-sdk-id'; + +export function getReconnectToken(): string | null { + const token = sessionStorage.getItem(STORAGE_KEY); + return token; +} + +export function setReconnectToken(token: string): void { + sessionStorage.setItem(STORAGE_KEY, token); +} + +export function clearReconnectToken(): void { + sessionStorage.removeItem(STORAGE_KEY); +} + +window.addEventListener('beforeunload', () => { + clearReconnectToken(); +});