diff --git a/lib/client.js b/lib/client.js index d6ab5f19..f5b5c36f 100644 --- a/lib/client.js +++ b/lib/client.js @@ -19,6 +19,16 @@ module.exports = function (dependencies) { function Client (options) { this.config = config(options); + this.healthCheckInterval = setInterval(() => { + if (this.session && !this.session.destroyed) { + this.session.ping((error, duration) => { + if (error) { + logger("No Ping response after " + duration + " ms"); + } + logger("Ping response after " + duration + " ms"); + }) + } + }, this.config.heartBeat).unref(); } Client.prototype.write = function write (notification, device, count) { @@ -43,6 +53,16 @@ module.exports = function (dependencies) { } }); + this.session.on("goaway", (errorCode, lastStreamId, opaqueData) => { + logger(`GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`); + // gracefully stop accepting new streams + if (this.session && !this.session.destroyed) { + this.session.close(() => { + this.session.destroy(); + }); + } + }); + if (logger.enabled) { this.session.on("connect", () => { logger("Session connected"); @@ -53,9 +73,6 @@ module.exports = function (dependencies) { this.session.on("frameError", (frameType, errorCode, streamId) => { logger(`Frame error: (frameType: ${frameType}, errorCode ${errorCode}, streamId: ${streamId})`); }); - this.session.on("goaway", (errorCode, lastStreamId, opaqueData) => { - logger(`GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`); - }); } } @@ -140,6 +157,9 @@ module.exports = function (dependencies) { }; Client.prototype.shutdown = function shutdown(callback) { + if (this.healthCheckInterval) { + clearInterval(this.healthCheckInterval); + } if (this.session && !this.session.destroyed) { this.session.shutdown({graceful: true}, () => { this.session.destroy();