diff --git a/src/constants.js b/src/constants.js index 470807b3..2a31c5f8 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,12 +1,12 @@ var SVGPATH = '', DEBUG = localStorage.ffzDebugMode == "true" && document.body.classList.contains('ffz-dev'), - WS_SERVER = DEBUG ? "localhost:8001" : "catbag.frankerfacez.com", + WS_SERVERS = DEBUG ? ["localhost:8001", "catbag.frankerfacez.com"] : ["catbag.frankerfacez.com"], SERVER = DEBUG ? "//localhost:8000/" : "//cdn.frankerfacez.com/"; module.exports = { DEBUG: DEBUG, SERVER: SERVER, - WS_SERVER: WS_SERVER, + WS_SERVERS: WS_SERVERS, API_SERVER: "//api.frankerfacez.com/", API_SERVER_2: "//direct-api.frankerfacez.com/", diff --git a/src/socket.js b/src/socket.js index 2eae3781..30882bb0 100644 --- a/src/socket.js +++ b/src/socket.js @@ -4,6 +4,7 @@ var FFZ = window.FrankerFaceZ, FFZ.prototype._ws_open = false; FFZ.prototype._ws_delay = 0; FFZ.prototype._ws_last_iframe = 0; +FFZ.prototype._ws_host_idx = 0; FFZ.ws_commands = {}; FFZ.ws_on_close = []; @@ -39,7 +40,7 @@ FFZ.prototype.ws_create = function() { this._ws_pending = this._ws_pending || []; try { - ws = this._ws_sock = new WebSocket("ws://" + constants.WS_SERVER + "/"); + ws = this._ws_sock = new WebSocket("ws://" + constants.WS_SERVERs[this._ws_host_idx] + "/"); } catch(err) { this._ws_exists = false; return this.log("Error Creating WebSocket: " + err); @@ -119,7 +120,7 @@ FFZ.prototype.ws_create = function() { f._ws_open = false; // When the connection closes, run our callbacks. - for(var i=0; i < FFZ.ws_on_close.length; i++) { + for (var i=0; i < FFZ.ws_on_close.length; i++) { try { FFZ.ws_on_close[i].bind(f)(); } catch(err) { @@ -127,6 +128,9 @@ FFZ.prototype.ws_create = function() { } } + // Attempt to cycle to backup server + f._ws_host_idx = (f._ws_host_idx + 1) % constants.WS_SERVERS.length; + if ( f._ws_delay > 10000 ) { var ua = navigator.userAgent.toLowerCase(); if ( Date.now() - f._ws_last_iframe > 1800000 && !(ua.indexOf('chrome') === -1 && ua.indexOf('safari') !== -1) )