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: propagate session id with error event #358

Merged
merged 2 commits into from
May 28, 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
13 changes: 6 additions & 7 deletions packages/js/src/Modules/Verto/BaseSession.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import * as log from 'loglevel';
import { v4 as uuidv4 } from 'uuid';
import logger from './util/logger';
import Connection from './services/Connection';
import BaseMessage from './messages/BaseMessage';
import Connection from './services/Connection';
import {
deRegister,
deRegisterAll,
register,
trigger,
deRegisterAll,
} from './services/Handler';
import { ADD, REMOVE, SwEvent } from './util/constants';
import { SwEvent } from './util/constants';
import { isFunction, isValidOptions, randomInt } from './util/helpers';
import { BroadcastParams, IVertoOptions } from './util/interfaces';
import { isFunction, randomInt, isValidOptions } from './util/helpers';
import { sessionStorage } from './util/storage';
import logger from './util/logger';

const KEEPALIVE_INTERVAL = 10 * 1000;

Expand Down Expand Up @@ -238,7 +237,7 @@ export default abstract class BaseSession {
* @return void
*/
protected _handleLoginError(error: any) {
trigger(SwEvent.Error, error, this.uuid);
trigger(SwEvent.Error, { error, sessionId: this.sessionid }, this.uuid);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/js/src/Modules/Verto/BrowserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from './webrtc/helpers';
import { findElementByType } from './util/helpers';
import { Unsubscribe, Subscribe, Broadcast } from './messages/Verto';
import { sessionStorage } from './util/storage';
import { stopStream } from './util/webrtc';
import { IWebRTCCall } from './webrtc/interfaces';
import Call from './webrtc/Call';
Expand Down Expand Up @@ -84,7 +83,6 @@ export default abstract class BrowserSession extends BaseSession {
* ```
*/
async connect(): Promise<void> {
this.sessionid = await sessionStorage.getItem(SESSION_ID);
super.connect();
}

Expand Down
6 changes: 2 additions & 4 deletions packages/js/src/Modules/Verto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
import { IVertoCallOptions } from './webrtc/interfaces';
import { Login } from './messages/Verto';
import Call from './webrtc/Call';
import { SESSION_ID, TIME_CALL_INVITE } from './util/constants';
import { sessionStorage } from './util/storage';
import { TIME_CALL_INVITE } from './util/constants';
import VertoHandler from './webrtc/VertoHandler';
import { isValidOptions } from './util/helpers';

Expand Down Expand Up @@ -42,7 +41,7 @@ export default class Verto extends BrowserSession {
throw new Error('Verto.newCall() error: destinationNumber is required.');
}

console.time(TIME_CALL_INVITE)
console.time(TIME_CALL_INVITE);
const call = new Call(this, options);
call.invite();
return call;
Expand Down Expand Up @@ -81,7 +80,6 @@ export default class Verto extends BrowserSession {
if (response) {
this._autoReconnect = autoReconnect;
this.sessionid = response.sessid;
sessionStorage.setItem(SESSION_ID, this.sessionid);
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/js/src/Modules/Verto/messages/verto/Login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BaseRequest from './BaseRequest';
import pkg from '../../../../../package.json';

class Login extends BaseRequest {
method: string = 'login';
Expand All @@ -19,7 +20,10 @@ class Login extends BaseRequest {
login_token,
userVariables,
loginParams: {},
'User-Agent': window.navigator.userAgent,
'User-Agent': {
sdkVersion: pkg.version,
data: navigator.userAgent,
},
};
if (sessionid) {
params.sessid = sessionid;
Expand Down
9 changes: 6 additions & 3 deletions packages/js/src/Modules/Verto/services/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ export default class Connection {
this._wsClient.onclose = (event): boolean =>
trigger(SwEvent.SocketClose, event, this.session.uuid);
this._wsClient.onerror = (event): boolean =>
trigger(SwEvent.SocketError, event, this.session.uuid);
trigger(
SwEvent.SocketError,
{ error: event, sessionId: this.session.sessionid },
this.session.uuid
);
this._wsClient.onmessage = (event): void => {
const msg: any = safeParseJson(event.data);
if (typeof msg === 'string') {
Expand All @@ -101,7 +105,7 @@ export default class Connection {
trigger(SwEvent.SocketMessage, msg, this.session.uuid);

// save previous gate state
if(Boolean(gateWayState)) {
if (Boolean(gateWayState)) {
this.previousGatewayState = gateWayState;
}
}
Expand Down Expand Up @@ -143,7 +147,6 @@ export default class Connection {
delete this._timers[id];
}


private _handleStringResponse(response: string) {
if (/^#SP/.test(response)) {
switch (response[3]) {
Expand Down
16 changes: 11 additions & 5 deletions packages/js/src/Modules/Verto/webrtc/BaseCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getUserMedia,
sdpToJsonHack,
setMediaElementSinkId,
stopStream
stopStream,
} from '../util/webrtc';
import Call from './Call';
import { MCULayoutEventHandler } from './LayoutHandler';
Expand Down Expand Up @@ -184,7 +184,6 @@ export default abstract class BaseCall implements IWebRTCCall {
}
}


get nodeId(): string {
return this._targetNodeId;
}
Expand Down Expand Up @@ -346,8 +345,12 @@ export default abstract class BaseCall implements IWebRTCCall {
});
this._execute(bye)
.catch((error) => {
logger.error('telnyl_rtc.bye failed!', error);
trigger(SwEvent.Error, error, this.session.uuid);
logger.error('telnyx_rtc.bye failed!', error);
trigger(
SwEvent.Error,
{ error, sessionId: this.session.sessionid },
this.session.uuid
);
})
.then(_close.bind(this));
} else {
Expand Down Expand Up @@ -1360,7 +1363,10 @@ export default abstract class BaseCall implements IWebRTCCall {
if (isFunction(this.peer.onSdpReadyTwice)) {
trigger(
SwEvent.Error,
new Error('SDP without candidates for the second time!'),
{
error: new Error('SDP without candidates for the second time!'),
sessionId: this.session.sessionid,
},
this.session.uuid
);
return;
Expand Down
40 changes: 29 additions & 11 deletions packages/js/src/Modules/Verto/webrtc/VertoHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import Call from './Call';
import { checkSubscribeResponse } from './helpers';
import { Result } from '../messages/Verto';
import { SwEvent } from '../util/constants';
import { VertoMethod, NOTIFICATION_TYPE, GatewayStateType, Direction } from './constants';
import {
VertoMethod,
NOTIFICATION_TYPE,
GatewayStateType,
Direction,
} from './constants';
import { trigger, deRegister } from '../services/Handler';
import { State, ConferenceAction } from './constants';
import { MCULayoutEventHandler } from './LayoutHandler';
Expand Down Expand Up @@ -125,7 +130,7 @@ class VertoHandler {
const call = _buildCall();
call.playRingtone();
call.setState(State.Ringing);
call.direction = Direction.Inbound
call.direction = Direction.Inbound;
this._ack(id, method);
break;
}
Expand Down Expand Up @@ -206,10 +211,13 @@ class VertoHandler {
VertoHandler.retriedRegister = 0;
trigger(
SwEvent.Error,
new ErrorResponse(
`Fail to register the user, the server tried ${RETRY_REGISTER_TIME} times`,
'UNREGED|NOREG'
),
{
error: new ErrorResponse(
`Fail to register the user, the server tried ${RETRY_REGISTER_TIME} times`,
'UNREGED|NOREG'
),
sessionId: session.sessionid,
},
session.uuid
);
break;
Expand All @@ -231,10 +239,13 @@ class VertoHandler {
VertoHandler.retriedConnect = 0;
trigger(
SwEvent.Error,
new ErrorResponse(
`Fail to connect the server, the server tried ${RETRY_CONNECT_TIME} times`,
'FAILED|FAIL_WAIT'
),
{
error: new ErrorResponse(
`Fail to connect the server, the server tried ${RETRY_CONNECT_TIME} times`,
'FAILED|FAIL_WAIT'
),
sessionId: session.sessionid,
},
session.uuid
);
break;
Expand All @@ -243,7 +254,14 @@ class VertoHandler {
VertoHandler.retriedConnect += 1;
if (VertoHandler.retriedConnect === RETRY_CONNECT_TIME) {
VertoHandler.retriedConnect = 0;
trigger(SwEvent.Error, params, session.uuid);
trigger(
SwEvent.Error,
{
error: new Error('Connection Retry Failed'),
sessionId: session.sessionid,
},
session.uuid
);
break;
} else {
setTimeout(() => {
Expand Down
Loading