Skip to content
Snippets Groups Projects
Commit abfcd34c authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] reopen websocket when closing by navigator or server - fix #535

[fix] Network: do not try to access a node without valid host - fix #537
parent e3e81f4d
Branches
Tags
No related merge requests found
......@@ -193,12 +193,12 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
// Override close methods (add a usage counter)
sock._counter = 1;
var close = sock.close;
var inheritedClose = sock.close;
sock.close = function() {
sock._counter--;
if (sock._counter <= 0) {
console.debug('[BMA] Stopping websocket ['+path+']');
close();
console.debug('[BMA] Closing websocket ['+path+']');
inheritedClose();
delete that.cache.wsByPath[path];
}
};
......
......@@ -63,6 +63,10 @@ angular.module('cesium.http.services', ['cesium.cache.services'])
}
function getResource(host, port, path, useSsl, forcedTimeout) {
// Make sure host is defined - fix #537
if (!host) {
return $q.reject('[http] invalid URL from host: ' + host);
}
var url = getUrl(host, port, path, useSsl);
return function(params) {
return $q(function(resolve, reject) {
......@@ -149,35 +153,64 @@ angular.module('cesium.http.services', ['cesium.cache.services'])
throw 'calling csHttp.ws without path argument';
}
var uri = getWsUrl(host, port, path, useSsl);
var sock = null;
var delegate = null;
var callbacks = [];
function _waitOpen() {
if (!sock) throw new Error('Websocket not opened');
if (sock.readyState == 1) {
return $q.when(sock);
if (!delegate) throw new Error('Websocket not opened');
if (delegate.readyState == 1) {
return $q.when(delegate);
}
if (sock.readyState == 3) {
return $q.reject('Unable to connect to Websocket ['+sock.url+']');
if (delegate.readyState == 3) {
return $q.reject('Unable to connect to Websocket ['+delegate.url+']');
}
return $timeout(_waitOpen, 100);
console.debug('[http] Waiting websocket ['+path+'] openning...');
return $timeout(_waitOpen, 200);
}
function _open(self, callback, params) {
if (!sock) {
if (!delegate) {
self.path = path;
prepare(uri, params, {}, function(uri) {
console.debug('[http] Listening on websocket ['+path+']...');
sock = new WebSocket(uri);
sock.onerror = function(e) {
sock.readyState=3;
delegate = new WebSocket(uri);
delegate.onerror = function(e) {
delegate.readyState=3;
};
sock.onmessage = function(e) {
delegate.onmessage = function(e) {
var obj = JSON.parse(e.data);
_.forEach(callbacks, function(callback) {
callback(obj);
});
};
delegate.onopen = function(e) {
console.debug('[http] Listening on websocket ['+path+']...');
sockets.push(self);
delegate.openTime = new Date().getTime();
};
delegate.onclose = function() {
// Remove from sockets arrays
var index = _.findIndex(sockets, function(socket){return socket.path === path;});
if (index >= 0) {
sockets.splice(index,1);
}
// If close event comes from Cesium
if (delegate.closing) {
delegate = null;
}
// If unexpected close event, reopen the socket (fix #535)
else {
console.debug('[http] Unexpected close of websocket ['+path+'] (open '+ (new Date().getTime() - delegate.openTime) +'ms ago): re-opening...');
delegate = null;
// Loop, but without the already registered callback
_open(self, null, params);
}
};
});
}
if (callback) callbacks.push(callback);
......@@ -194,14 +227,14 @@ angular.module('cesium.http.services', ['cesium.cache.services'])
send: function(data) {
return _waitOpen()
.then(function(){
sock.send(data);
delegate.send(data);
});
},
close: function() {
if (sock) {
console.debug('[http] Stopping websocket ['+path+']...');
sock.close();
sock = null;
if (delegate) {
delegate.closing = true;
console.debug('[http] Closing websocket ['+path+']...');
delegate.close();
callbacks = [];
}
}
......
......@@ -308,7 +308,7 @@ angular.module('cesium.network.services', ['ngApi', 'cesium.bma.services', 'cesi
// Apply filter
if (!applyPeerFilter(peer)) return $q.when();
if (!data.filter.online || (data.filter.online === 'all' && peer.status === 'DOWN')) {
if (!data.filter.online || (data.filter.online === 'all' && peer.status === 'DOWN') || !peer.getHost() /*fix #537*/) {
peer.online = false;
return $q.when(peer);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment