mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +00:00
add optional ws ping (#370)
This commit is contained in:
@@ -51,7 +51,7 @@ const JAMBONES_SBCS = process.env.JAMBONES_SBCS;
|
|||||||
/* websockets */
|
/* websockets */
|
||||||
const JAMBONES_WS_HANDSHAKE_TIMEOUT_MS = parseInt(process.env.JAMBONES_WS_HANDSHAKE_TIMEOUT_MS, 10) || 1500;
|
const JAMBONES_WS_HANDSHAKE_TIMEOUT_MS = parseInt(process.env.JAMBONES_WS_HANDSHAKE_TIMEOUT_MS, 10) || 1500;
|
||||||
const JAMBONES_WS_MAX_PAYLOAD = parseInt(process.env.JAMBONES_WS_MAX_PAYLOAD, 10) || 24 * 1024;
|
const JAMBONES_WS_MAX_PAYLOAD = parseInt(process.env.JAMBONES_WS_MAX_PAYLOAD, 10) || 24 * 1024;
|
||||||
|
const JAMBONES_WS_PING_INTERVAL_MS = parseInt(process.env.JAMBONES_WS_PING_INTERVAL_MS, 10) || 0;
|
||||||
const MAX_RECONNECTS = 5;
|
const MAX_RECONNECTS = 5;
|
||||||
const RESPONSE_TIMEOUT_MS = parseInt(process.env.JAMBONES_WS_API_MSG_RESPONSE_TIMEOUT, 10) || 5000;
|
const RESPONSE_TIMEOUT_MS = parseInt(process.env.JAMBONES_WS_API_MSG_RESPONSE_TIMEOUT, 10) || 5000;
|
||||||
|
|
||||||
@@ -186,6 +186,7 @@ module.exports = {
|
|||||||
RESPONSE_TIMEOUT_MS,
|
RESPONSE_TIMEOUT_MS,
|
||||||
JAMBONES_WS_HANDSHAKE_TIMEOUT_MS,
|
JAMBONES_WS_HANDSHAKE_TIMEOUT_MS,
|
||||||
JAMBONES_WS_MAX_PAYLOAD,
|
JAMBONES_WS_MAX_PAYLOAD,
|
||||||
|
JAMBONES_WS_PING_INTERVAL_MS,
|
||||||
MAX_RECONNECTS,
|
MAX_RECONNECTS,
|
||||||
GCP_JSON_KEY,
|
GCP_JSON_KEY,
|
||||||
MICROSOFT_REGION,
|
MICROSOFT_REGION,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const Websocket = require('ws');
|
|||||||
const snakeCaseKeys = require('./snakecase-keys');
|
const snakeCaseKeys = require('./snakecase-keys');
|
||||||
const {
|
const {
|
||||||
RESPONSE_TIMEOUT_MS,
|
RESPONSE_TIMEOUT_MS,
|
||||||
|
JAMBONES_WS_PING_INTERVAL_MS,
|
||||||
MAX_RECONNECTS,
|
MAX_RECONNECTS,
|
||||||
JAMBONES_WS_HANDSHAKE_TIMEOUT_MS,
|
JAMBONES_WS_HANDSHAKE_TIMEOUT_MS,
|
||||||
JAMBONES_WS_MAX_PAYLOAD
|
JAMBONES_WS_MAX_PAYLOAD
|
||||||
@@ -178,9 +179,17 @@ class WsRequestor extends BaseRequestor {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_stopPingTimer() {
|
||||||
|
if (this._pingTimer) {
|
||||||
|
clearInterval(this._pingTimer);
|
||||||
|
this._pingTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
this.closedGracefully = true;
|
this.closedGracefully = true;
|
||||||
this.logger.debug('WsRequestor:close closing socket');
|
this.logger.debug('WsRequestor:close closing socket');
|
||||||
|
this._stopPingTimer();
|
||||||
try {
|
try {
|
||||||
if (this.ws) {
|
if (this.ws) {
|
||||||
this.ws.close(1000);
|
this.ws.close(1000);
|
||||||
@@ -195,6 +204,7 @@ class WsRequestor extends BaseRequestor {
|
|||||||
|
|
||||||
_connect() {
|
_connect() {
|
||||||
assert(!this.ws);
|
assert(!this.ws);
|
||||||
|
this._stopPingTimer();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const handshakeTimeout = JAMBONES_WS_HANDSHAKE_TIMEOUT_MS ?
|
const handshakeTimeout = JAMBONES_WS_HANDSHAKE_TIMEOUT_MS ?
|
||||||
parseInt(JAMBONES_WS_HANDSHAKE_TIMEOUT_MS) :
|
parseInt(JAMBONES_WS_HANDSHAKE_TIMEOUT_MS) :
|
||||||
@@ -255,10 +265,15 @@ class WsRequestor extends BaseRequestor {
|
|||||||
this.connectInProgress = false;
|
this.connectInProgress = false;
|
||||||
this.connections++;
|
this.connections++;
|
||||||
this.emit('ready', ws);
|
this.emit('ready', ws);
|
||||||
|
|
||||||
|
if (JAMBONES_WS_PING_INTERVAL_MS > 15000) {
|
||||||
|
this._pingTimer = setInterval(() => this.ws?.ping(), JAMBONES_WS_PING_INTERVAL_MS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_onClose(code) {
|
_onClose(code) {
|
||||||
this.logger.info(`WsRequestor(${this.id}) - closed from far end ${code}`);
|
this.logger.info(`WsRequestor(${this.id}) - closed from far end ${code}`);
|
||||||
|
this._stopPingTimer();
|
||||||
if (this.connections > 0 && code !== 1000) {
|
if (this.connections > 0 && code !== 1000) {
|
||||||
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');
|
||||||
@@ -283,6 +298,7 @@ class WsRequestor extends BaseRequestor {
|
|||||||
_onSocketClosed() {
|
_onSocketClosed() {
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
this.emit('connection-dropped');
|
this.emit('connection-dropped');
|
||||||
|
this._stopPingTimer();
|
||||||
if (this.connections > 0 && this.connections < MAX_RECONNECTS && !this.closedGracefully) {
|
if (this.connections > 0 && this.connections < MAX_RECONNECTS && !this.closedGracefully) {
|
||||||
if (!this._initMsgId) this._clearPendingMessages();
|
if (!this._initMsgId) this._clearPendingMessages();
|
||||||
this.logger.debug(`WsRequestor:_onSocketClosed waiting ${this.backoffMs} to reconnect`);
|
this.logger.debug(`WsRequestor:_onSocketClosed waiting ${this.backoffMs} to reconnect`);
|
||||||
|
|||||||
Reference in New Issue
Block a user