Skip to content

Commit

Permalink
feat(WEBRTC-232): remove refreshToken and blade methods #75
Browse files Browse the repository at this point in the history
  • Loading branch information
DeividVeloso authored Dec 3, 2020
1 parent 1aad4a1 commit a2ce717
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 101 deletions.
105 changes: 5 additions & 100 deletions packages/js/src/Modules/Verto/BaseSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ import * as log from 'loglevel';
import { v4 as uuidv4 } from 'uuid';
import logger from './util/logger';
import Connection from './services/Connection';
import Setup from './services/Setup';
import BaseMessage from './messages/BaseMessage';
import {
deRegister,
register,
trigger,
deRegisterAll,
} from './services/Handler';
import BroadcastHandler from './services/BroadcastHandler';
import { ADD, REMOVE, SwEvent, BladeMethod } from './util/constants';
import { NOTIFICATION_TYPE } from './webrtc/constants';
import { ADD, REMOVE, SwEvent } from './util/constants';
import {
BroadcastParams,
ITelnyxRTCOptions,
SubscribeParams,
IBladeConnectResult,
} from './util/interfaces';
import { Subscription, Connect, Reauthenticate, Ping } from './messages/Blade';
import { isFunction, randomInt } from './util/helpers';
import { Subscription, Ping } from './messages/Blade';
import { isFunction, randomInt, isValidOptions } from './util/helpers';
import { sessionStorage } from './util/storage';
import { isValidOptions } from './util/helpers';

const KEEPALIVE_INTERVAL = 10 * 1000;

Expand Down Expand Up @@ -57,7 +52,6 @@ export default abstract class BaseSession {
this._onSocketCloseOrError = this._onSocketCloseOrError.bind(this);
this._onSocketMessage = this._onSocketMessage.bind(this);
this._handleLoginError = this._handleLoginError.bind(this);
this._checkTokenExpiration = this._checkTokenExpiration.bind(this);

this._attachListeners();
this.connection = new Connection(this);
Expand Down Expand Up @@ -246,31 +240,6 @@ export default abstract class BaseSession {
return this;
}

/**
* Refresh the
* @return void
*/
async refreshToken(token: string) {
this.options.token = token;
try {
if (this.expired) {
await this.connect();
} else {
const br = new Reauthenticate(
this.options.project,
token,
this.sessionid
);
const response = await this.execute(br);
const { authorization: { expires_at = null } = {} } = response;
this.expiresAt = +expires_at || 0;
}
} catch (error) {
logger.error('refreshToken error:', error);
trigger(SwEvent.Error, error, this.uuid, false);
}
}

/**
* Define the method to connect the session
* @abstract
Expand Down Expand Up @@ -300,36 +269,7 @@ export default abstract class BaseSession {
* Callback when the ws connection is open
* @return void
*/
protected async _onSocketOpen() {
this._idle = false;
const tokenKey = this._jwtAuth ? 'jwt_token' : 'token';
const { project, token } = this.options;
const bc = new Connect({ project, [tokenKey]: token }, this.sessionid);
const response: IBladeConnectResult = await this.execute(bc).catch(
this._handleLoginError
);
if (response) {
this._autoReconnect = true;
const {
sessionid,
nodeid,
master_nodeid,
authorization: { expires_at = null, signature = null } = {},
} = response;
this.expiresAt = +expires_at || 0;
this.signature = signature;
this.relayProtocol = await Setup(this);
this._checkTokenExpiration();
this.sessionid = sessionid;
this.nodeid = nodeid;
this.master_nodeid = master_nodeid;
this._emptyExecuteQueues();
this._pong = null;
this._keepAlive();
trigger(SwEvent.Ready, this, this.uuid);
logger.info('Session Ready!');
}
}
protected async _onSocketOpen() {}

/**
* Callback when the ws connection is going to close or get an error
Expand Down Expand Up @@ -362,17 +302,7 @@ export default abstract class BaseSession {
* Callback to handle inbound messages from the ws
* @return void
*/
protected _onSocketMessage(response: any) {
const { method, params } = response;
switch (method) {
case BladeMethod.Broadcast:
BroadcastHandler(this, params);
break;
case BladeMethod.Disconnect:
this._idle = true;
break;
}
}
protected _onSocketMessage(response: any) {}

/**
* Remove subscription by key and deregister the related callback
Expand Down Expand Up @@ -477,31 +407,6 @@ export default abstract class BaseSession {
}
}

/**
* Set a timer to dispatch a notification when the JWT is going to expire.
* @return void
*/
private _checkTokenExpiration() {
if (!this.expiresAt) {
return;
}
const diff = this.expiresAt - Date.now() / 1000;
if (diff <= 60) {
logger.warn(
'Your JWT is going to expire. You should refresh it to keep the session live.'
);
trigger(
SwEvent.Notification,
{ type: NOTIFICATION_TYPE.refreshToken, session: this },
this.uuid,
false
);
}
if (!this.expired) {
setTimeout(this._checkTokenExpiration, 30 * 1000);
}
}

private _keepAlive() {
if (this._doKeepAlive !== true) {
return;
Expand Down
2 changes: 2 additions & 0 deletions packages/js/src/Modules/Verto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { trigger } from './services/Handler';
import { sessionStorage } from './util/storage';
import VertoHandler from './webrtc/VertoHandler';
import { isValidOptions } from './util/helpers';
import logger from './util/logger';

export const VERTO_PROTOCOL = 'verto-protocol';

Expand Down Expand Up @@ -64,6 +65,7 @@ export default class Verto extends BrowserSession {
this.sessionid = response.sessid;
sessionStorage.setItem(SESSION_ID, this.sessionid);
trigger(SwEvent.Ready, this, this.uuid);
logger.info('Session Ready!');
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/js/src/Modules/Verto/webrtc/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const NOTIFICATION_TYPE = {
callUpdate: 'callUpdate',
vertoClientReady: 'vertoClientReady',
userMediaError: 'userMediaError',
refreshToken: 'refreshToken',
};

export const DEFAULT_CALL_OPTIONS: CallOptions = {
Expand Down

0 comments on commit a2ce717

Please sign in to comment.