Skip to content

Commit

Permalink
add support for session:redirect event
Browse files Browse the repository at this point in the history
  • Loading branch information
davehorton committed Dec 31, 2024
1 parent cccd471 commit e6fa14a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Client extends Emitter {
case 'session:adulting':
this._onSessionAdulting(ws, path, msg);
break;
case 'session:redirect':
this._onSessionRedirect(ws, path, msg);
break;
default:
debug(`Client: discarding msg type ${msg.type}`);
}
Expand Down Expand Up @@ -61,6 +64,23 @@ class Client extends Emitter {
ws.off('message', this._boundHandler);
this.emit('session:new', session, path, ws.req);
}

_onSessionRedirect(ws, path, msg) {
const logger = typeof this.logger.child === 'function' ?
this.logger.child({call_sid: msg.call_sid}) :
this.logger;
const session = new Session({logger, ws, msg});
/* Note: all further messaging after session:new will be handled by the session */
ws.off('message', this._boundHandler);
session.reply();

// Check if there are listeners for "session:redirect", otherwise emit "session:new"
if (this.listenerCount('session:redirect') > 0) {
this.emit('session:redirect', session, path, ws.req);
} else {
this.emit('session:new', session, path, ws.req);
}
}
}

module.exports = Client;

0 comments on commit e6fa14a

Please sign in to comment.