Skip to content

Commit

Permalink
fix: reinitialize PepTalk client to reconnect
Browse files Browse the repository at this point in the history
might help reconnect properly
  • Loading branch information
ianshade committed Jun 4, 2021
1 parent 37cacf3 commit fa19cdc
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/mse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ export class MSERep extends EventEmitter implements MSE {
this.restPort = typeof restPort === 'number' && restPort > 0 ? restPort : 8580
this.wsPort = typeof wsPort === 'number' && wsPort > 0 ? wsPort : 8595
this.resthost = resthost // For ngrok testing only
this.pep = startPepTalk(this.hostname, this.wsPort)
this.pep.on('close', () => this.onPepClose())
this.connection = this.pep.connect().catch((e) => e)
this.pep = this.initPep()
}

initPep() {
const pep = startPepTalk(this.hostname, this.wsPort)
pep.on('close', () => this.onPepClose())
this.connection = pep.connect().catch((e) => e)
return pep
}

async onPepClose(): Promise<void> {
if (!this.reconnectTimeout) {
this.connection = undefined
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = undefined
this.connection = this.pep.connect().catch((e) => e)
this.pep.removeAllListeners()
this.pep = this.initPep()
this.lastReconnectTime = Date.now()
}, Math.max(2000 - (Date.now() - this.lastReconnectTime), 0))
}
Expand Down

0 comments on commit fa19cdc

Please sign in to comment.