Skip to content

Commit

Permalink
feat: attach reconnection token to host (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
farhat-ha authored Aug 14, 2024
1 parent bf16710 commit 27be846
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/js/src/Modules/Verto/services/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
Expand All @@ -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');

Expand Down
18 changes: 18 additions & 0 deletions packages/js/src/Modules/Verto/util/reconnect.ts
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 27be846

Please sign in to comment.