* Attempt a reconnection. * * @private
()
| 574 | * @private |
| 575 | */ |
| 576 | private reconnect(): this | void { |
| 577 | if (this._reconnecting || this.skipReconnect) return this; |
| 578 | |
| 579 | const self = this; |
| 580 | |
| 581 | if (this.backoff.attempts >= this._reconnectionAttempts) { |
| 582 | debug(class="st">"reconnect failed"); |
| 583 | this.backoff.reset(); |
| 584 | this.emitReserved(class="st">"reconnect_failed"); |
| 585 | this._reconnecting = false; |
| 586 | } else { |
| 587 | const delay = this.backoff.duration(); |
| 588 | debug(class="st">"will wait %dms before reconnect attempt", delay); |
| 589 | |
| 590 | this._reconnecting = true; |
| 591 | const timer = this.setTimeoutFn(() => { |
| 592 | if (self.skipReconnect) return; |
| 593 | |
| 594 | debug(class="st">"attempting reconnect"); |
| 595 | this.emitReserved(class="st">"reconnect_attempt", self.backoff.attempts); |
| 596 | |
| 597 | class="cm">// check again for the case socket closed in above events |
| 598 | if (self.skipReconnect) return; |
| 599 | |
| 600 | self.open((err) => { |
| 601 | if (err) { |
| 602 | debug(class="st">"reconnect attempt error"); |
| 603 | self._reconnecting = false; |
| 604 | self.reconnect(); |
| 605 | this.emitReserved(class="st">"reconnect_error", err); |
| 606 | } else { |
| 607 | debug(class="st">"reconnect success"); |
| 608 | self.onreconnect(); |
| 609 | } |
| 610 | }); |
| 611 | }, delay); |
| 612 | |
| 613 | if (this.opts.autoUnref) { |
| 614 | timer.unref(); |
| 615 | } |
| 616 | |
| 617 | this.subs.push(() => { |
| 618 | this.clearTimeoutFn(timer); |
| 619 | }); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Called upon successful reconnect. |
no test coverage detected