Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: attach reconnection token to host #367

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
Loading