mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +00:00
add support for session:reconnect over ws api
This commit is contained in:
@@ -40,6 +40,8 @@ class WsRequestor extends BaseRequestor {
|
||||
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 (this._isAbsoluteUrl(url) && url.startsWith('http')) {
|
||||
this.logger.debug({hook}, 'WsRequestor: sending a webhook');
|
||||
@@ -72,6 +74,7 @@ class WsRequestor extends BaseRequestor {
|
||||
const obj = {
|
||||
type,
|
||||
msgid,
|
||||
call_sid: this.call_sid,
|
||||
hook: type === 'verb:hook' ? url : undefined,
|
||||
data: {...payload}
|
||||
};
|
||||
@@ -137,7 +140,7 @@ class WsRequestor extends BaseRequestor {
|
||||
.once('ready', (ws) => {
|
||||
this.ws = ws;
|
||||
this.removeAllListeners('not-ready');
|
||||
this.connections++;
|
||||
if (this.connections++ > 0) this.request('session:reconnect', this.url);
|
||||
resolve();
|
||||
})
|
||||
.once('not-ready', (err) => {
|
||||
@@ -151,22 +154,22 @@ class WsRequestor extends BaseRequestor {
|
||||
|
||||
_setHandlers(ws) {
|
||||
ws
|
||||
.on('open', this._onOpen.bind(this, ws))
|
||||
.on('close', this._onClose.bind(this))
|
||||
.once('open', this._onOpen.bind(this, ws))
|
||||
.once('close', this._onClose.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));
|
||||
}
|
||||
|
||||
_onError(err) {
|
||||
if (this.connections > 0) {
|
||||
this.logger.info({url: this.url, err}, 'WsRequestor:_onError');
|
||||
if (this.connections > 0) this.emit('socket-closed');
|
||||
}
|
||||
else this.emit('not-ready', err);
|
||||
}
|
||||
|
||||
_onOpen(ws) {
|
||||
if (this.ws) this.logger.info({old_ws: this.ws._socket.address()}, 'WsRequestor:_onOpen');
|
||||
assert(!this.ws);
|
||||
this.emit('ready', ws);
|
||||
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.emit('socket-closed');
|
||||
}
|
||||
this.ws && this.ws.removeAllListeners();
|
||||
this.ws = null;
|
||||
}
|
||||
|
||||
_onUnexpectedResponse(ws, req, res) {
|
||||
@@ -193,8 +198,11 @@ class WsRequestor extends BaseRequestor {
|
||||
_onSocketClosed() {
|
||||
this.ws = null;
|
||||
if (this.connections > 0) {
|
||||
if (++this.connections < MAX_RECONNECTS) {
|
||||
setImmediate(this.connect.bind(this));
|
||||
if (this.connections < MAX_RECONNECTS) {
|
||||
setTimeout(this._connect.bind(this), 500);
|
||||
}
|
||||
else {
|
||||
this.logger.info('WsRequestor:_onSocketClosed - max reconnection attempts reached');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user