Skip to content

Commit

Permalink
* (Apollon77) Optimize reconnection handling for push connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Sep 12, 2023
1 parent 5de34f1 commit 22cfcef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Then it should work again
* docu docu docu (sorry ... will come)

## Changelog:

### __WORK IN PROGRESS__
* (Apollon77) Optimize reconnection handling for push connections

### 6.1.1 (2023-09-09)
* (Apollon77) Fix for cookie refresh check

Expand Down
16 changes: 9 additions & 7 deletions alexa-http2push.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ class AlexaHttp2Push extends EventEmitter {
reason = reason.toString();
}
try {
this.stream && this.stream.end();
this.stream && this.stream.destroy();
} catch (err) {
// ignore
}
try {
this.client && this.client.close();
this.client && this.client.destroy();
} catch (err) {
// ignore
}
Expand Down Expand Up @@ -97,15 +97,15 @@ class AlexaHttp2Push extends EventEmitter {

const retryDelay = (immediateReconnect || this.errorRetryCounter === 1) ? 1 : Math.min(60, this.errorRetryCounter * 5);
this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Retry Connection in ' + retryDelay + 's');
this.emit('disconnect', true, 'Retry Connection in ' + retryDelay + 's');
this.emit('disconnect', true, `Retry Connection in ${retryDelay}s (${code}: ${reason})`);
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connect();
}, retryDelay * 1000);
};

const onPingResponse = () => {
const onPingResponse = (resetErrorCount) => {
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = null;
Expand All @@ -117,7 +117,9 @@ class AlexaHttp2Push extends EventEmitter {
this.pongTimeout = null;
}
this.connectionActive = true;
this.errorRetryCounter = 0;
if (resetErrorCount) {
this.errorRetryCounter = 0;
}
};

try {
Expand Down Expand Up @@ -147,13 +149,13 @@ class AlexaHttp2Push extends EventEmitter {
}
chunk = chunk.toString();
if (chunk.startsWith('------')) {
this.client.ping(onPingResponse);
this.client.ping(() => onPingResponse(false));

this.pingPongInterval = setInterval(() => {
if (!this.stream || !this.client) return;
this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Send Ping');
//console.log('SEND: ' + msg.toString('hex'));
this.client.ping(onPingResponse);
this.client.ping(() => onPingResponse(true));

this.pongTimeout = setTimeout(() => {
this.pongTimeout = null;
Expand Down

0 comments on commit 22cfcef

Please sign in to comment.