add support for session:reconnect over ws api

This commit is contained in:
Dave Horton
2022-02-27 16:57:00 -05:00
parent f317fbaa45
commit fb25389cd1

View File

@@ -40,6 +40,8 @@ class WsRequestor extends BaseRequestor {
return; return;
} }
if (type === 'session:new') this.call_sid = params.callSid;
/* if we have an absolute url, and it is http then do a standard webhook */ /* if we have an absolute url, and it is http then do a standard webhook */
if (this._isAbsoluteUrl(url) && url.startsWith('http')) { if (this._isAbsoluteUrl(url) && url.startsWith('http')) {
this.logger.debug({hook}, 'WsRequestor: sending a webhook'); this.logger.debug({hook}, 'WsRequestor: sending a webhook');
@@ -72,6 +74,7 @@ class WsRequestor extends BaseRequestor {
const obj = { const obj = {
type, type,
msgid, msgid,
call_sid: this.call_sid,
hook: type === 'verb:hook' ? url : undefined, hook: type === 'verb:hook' ? url : undefined,
data: {...payload} data: {...payload}
}; };
@@ -137,7 +140,7 @@ class WsRequestor extends BaseRequestor {
.once('ready', (ws) => { .once('ready', (ws) => {
this.ws = ws; this.ws = ws;
this.removeAllListeners('not-ready'); this.removeAllListeners('not-ready');
this.connections++; if (this.connections++ > 0) this.request('session:reconnect', this.url);
resolve(); resolve();
}) })
.once('not-ready', (err) => { .once('not-ready', (err) => {
@@ -151,22 +154,22 @@ class WsRequestor extends BaseRequestor {
_setHandlers(ws) { _setHandlers(ws) {
ws ws
.on('open', this._onOpen.bind(this, ws)) .once('open', this._onOpen.bind(this, ws))
.on('close', this._onClose.bind(this)) .once('close', this._onClose.bind(this))
.on('message', this._onMessage.bind(this)) .on('message', this._onMessage.bind(this))
.on('unexpected-response', this._onUnexpectedResponse.bind(this, ws)) .once('unexpected-response', this._onUnexpectedResponse.bind(this, ws))
.on('error', this._onError.bind(this)); .on('error', this._onError.bind(this));
} }
_onError(err) { _onError(err) {
if (this.connections > 0) { if (this.connections > 0) {
this.logger.info({url: this.url, err}, 'WsRequestor:_onError'); this.logger.info({url: this.url, err}, 'WsRequestor:_onError');
if (this.connections > 0) this.emit('socket-closed');
} }
else this.emit('not-ready', err); else this.emit('not-ready', err);
} }
_onOpen(ws) { _onOpen(ws) {
if (this.ws) this.logger.info({old_ws: this.ws._socket.address()}, 'WsRequestor:_onOpen');
assert(!this.ws); assert(!this.ws);
this.emit('ready', ws); this.emit('ready', ws);
this.logger.info({url: this.url}, 'WsRequestor - successfully connected'); this.logger.info({url: this.url}, 'WsRequestor - successfully connected');
@@ -177,6 +180,8 @@ class WsRequestor extends BaseRequestor {
this.logger.info({url: this.url}, 'WsRequestor - socket closed unexpectedly from remote side'); this.logger.info({url: this.url}, 'WsRequestor - socket closed unexpectedly from remote side');
this.emit('socket-closed'); this.emit('socket-closed');
} }
this.ws && this.ws.removeAllListeners();
this.ws = null;
} }
_onUnexpectedResponse(ws, req, res) { _onUnexpectedResponse(ws, req, res) {
@@ -193,8 +198,11 @@ class WsRequestor extends BaseRequestor {
_onSocketClosed() { _onSocketClosed() {
this.ws = null; this.ws = null;
if (this.connections > 0) { if (this.connections > 0) {
if (++this.connections < MAX_RECONNECTS) { if (this.connections < MAX_RECONNECTS) {
setImmediate(this.connect.bind(this)); setTimeout(this._connect.bind(this), 500);
}
else {
this.logger.info('WsRequestor:_onSocketClosed - max reconnection attempts reached');
} }
} }
} }