Created
April 19, 2016 02:11
-
-
Save copygirl/1374db592d6c86cb2d3afe1d95a914c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| let Plug = require("./Plug"); | |
| let { extend } = require("../utility"); | |
| let defaults = { | |
| initial: 2, // Starting delay in seconds. | |
| factor: 2, // After a failed reconnection attempt, multiply current delay by this factor. | |
| maximum: 64, // Maximum delay between reconnection attempts in seconds. | |
| retries: null // Maximum number of retries after giving up completely (null = retry FOREVER!!). | |
| }; | |
| module.exports = class Respawn extends Plug { | |
| constructor(cord, config) { | |
| super(cord, extend({ }, defaults, config)); | |
| } | |
| activate() { | |
| this.cord.on("disconnected", (socket) => { | |
| if (socket.reconnecting) return; | |
| this.reconnect(socket); | |
| }); | |
| } | |
| reconnect(socket, delay = this.config.initial, attempt = 1) { | |
| socket.reconnecting = true; | |
| if ((this.config.retries != null) && (attempt > this.config.retries)) | |
| return this.log(`Gave up reconnecting to ${ socket } after ${ attempt } attempts.`); | |
| let s = Math.round(delay); | |
| this.log(`Reconnecting in ${ s } second${ ((s == 1) ? "" : "s") }...`) | |
| setTimeout(() => { | |
| delay = Math.min(delay * this.config.factor, this.config.maximum); | |
| socket.connect().then( | |
| () => { socket.reconnecting = false; }, | |
| (reason) => this.reconnect(socket, delay, attempt + 1) | |
| ); | |
| }, delay * 1000); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment