mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-28 16:15:49 +00:00
[FEATURE] reconnect servers when browser comes online
This commit is contained in:
committed by
Geert Weening
parent
fe4cf94b62
commit
4e13170123
@@ -101,6 +101,7 @@ function Remote(opts, trace) {
|
|||||||
this._transaction_subs = 0;
|
this._transaction_subs = 0;
|
||||||
this._connection_count = 0;
|
this._connection_count = 0;
|
||||||
this._connected = false;
|
this._connected = false;
|
||||||
|
this._persist = false;
|
||||||
|
|
||||||
this._connection_offset = 1000 * (typeof opts.connection_offset === 'number' ? opts.connection_offset : 0);
|
this._connection_offset = 1000 * (typeof opts.connection_offset === 'number' ? opts.connection_offset : 0);
|
||||||
this._submission_timeout = 1000 * (typeof opts.submission_timeout === 'number' ? opts.submission_timeout : 10);
|
this._submission_timeout = 1000 * (typeof opts.submission_timeout === 'number' ? opts.submission_timeout : 10);
|
||||||
@@ -249,6 +250,32 @@ function Remote(opts, trace) {
|
|||||||
self._pingInterval = setInterval(pingServers, opts.ping * 1000);
|
self._pingInterval = setInterval(pingServers, opts.ping * 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if we are using a browser, reconnect
|
||||||
|
//the servers whenever the network comes online
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
if (window.addEventListener) { // W3C DOM
|
||||||
|
window.addEventListener('online', reconnect);
|
||||||
|
} else if (window.attachEvent) { // IE DOM
|
||||||
|
window.attachEvent('ononline', reconnect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function reconnect() {
|
||||||
|
if (!self._persist) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info('reconnecting');
|
||||||
|
|
||||||
|
;(function nextServer(i) {
|
||||||
|
self._servers[i].reconnect();
|
||||||
|
var next = nextServer.bind(this, ++i);
|
||||||
|
if (i < self._servers.length) {
|
||||||
|
setTimeout(next, self._connection_offset);
|
||||||
|
}
|
||||||
|
})(0);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(Remote, EventEmitter);
|
util.inherits(Remote, EventEmitter);
|
||||||
@@ -514,6 +541,7 @@ Remote.prototype.connect = function(online) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
this._persist = true;
|
||||||
|
|
||||||
;(function nextServer(i) {
|
;(function nextServer(i) {
|
||||||
self._servers[i].connect();
|
self._servers[i].connect();
|
||||||
@@ -542,6 +570,7 @@ Remote.prototype.disconnect = function(callback) {
|
|||||||
this.once('disconnect', callback);
|
this.once('disconnect', callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._persist = false;
|
||||||
this._servers.forEach(function(server) {
|
this._servers.forEach(function(server) {
|
||||||
server.disconnect();
|
server.disconnect();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user