Added transport initialization phase. Websocket transport is not skipped anymore if jsonp is not able to connect.

This commit is contained in:
slnpacifist 2016-04-13 17:23:19 +08:00
parent 864bc8a5a6
commit 10727359be
3 changed files with 46 additions and 5 deletions

View File

@ -43,6 +43,9 @@ function(syscall, globalTimeout, alerts, utils, rootScope, uri, authconf) {
} }
// set if we got error on connection. This will cause another connection attempt.
var needNewConnection = true;
// update is implemented such that // update is implemented such that
// only one syscall at max is ongoing // only one syscall at max is ongoing
// (i.e. serially) so should be private // (i.e. serially) so should be private
@ -57,11 +60,18 @@ function(syscall, globalTimeout, alerts, utils, rootScope, uri, authconf) {
}); });
var subs = subscriptions.slice(); var subs = subscriptions.slice();
if (!subs.length) { if (!subs.length) {
timeout = setTimeout(update, time); timeout = setTimeout(update, globalTimeout);
return; return;
} }
if (configurations.length) { if (syscall.state == 'initializing') {
console.log("Syscall is initializing, waiting");
timeout = setTimeout(update, globalTimeout);
return;
}
if (needNewConnection && configurations.length) {
needNewConnection = false;
currentConf = configurations[0]; currentConf = configurations[0];
if (currentConf && currentConf.auth && currentConf.auth.token) { if (currentConf && currentConf.auth && currentConf.auth.token) {
currentToken = currentConf.auth.token; currentToken = currentConf.auth.token;
@ -70,6 +80,8 @@ function(syscall, globalTimeout, alerts, utils, rootScope, uri, authconf) {
currentToken = null; currentToken = null;
} }
syscall.init(currentConf); syscall.init(currentConf);
timeout = setTimeout(update, globalTimeout);
return;
} }
var params = _.map(subs, function(s) { var params = _.map(subs, function(s) {
@ -84,6 +96,7 @@ function(syscall, globalTimeout, alerts, utils, rootScope, uri, authconf) {
}); });
var error = function() { var error = function() {
needNewConnection = true;
var ind = configurations.indexOf(currentConf); var ind = configurations.indexOf(currentConf);
if (ind != -1) configurations.splice(ind, 1); if (ind != -1) configurations.splice(ind, 1);

View File

@ -28,17 +28,29 @@ function(_, JSON, name, utils, alerts) {
_.each(sockRPC.handles, function(h) { h.error() }); _.each(sockRPC.handles, function(h) { h.error() });
sockRPC.handles = []; sockRPC.handles = [];
sockRPC.initialized = false; sockRPC.initialized = false;
if (sockRPC.onready) {
sockRPC.onready();
sockRPC.onready = null;
}
}, },
onclose: function(ev) { onclose: function(ev) {
if (sockRPC.handles && sockRPC.handles.length) if (sockRPC.handles && sockRPC.handles.length)
sockRPC.onerror('Connection reset while calling aria2'); sockRPC.onerror('Connection reset while calling aria2');
sockRPC.initialized = false; sockRPC.initialized = false;
if (sockRPC.onready) {
sockRPC.onready();
sockRPC.onready = null;
}
}, },
// when connection opens // when connection opens
onopen: function() { onopen: function() {
console.log('websocket initialized!!!'); console.log('websocket initialized!!!');
sockRPC.initialized = true; sockRPC.initialized = true;
if (sockRPC.onready) {
sockRPC.onready();
sockRPC.onready = null;
}
}, },
@ -77,11 +89,18 @@ function(_, JSON, name, utils, alerts) {
}, },
// should be called initially to start using the sock rpc // should be called initially to start using the sock rpc
init: function(conf) { // onready is called when initial connection is resolved
init: function(conf, onready) {
sockRPC.initialized = false; sockRPC.initialized = false;
if (sockRPC.onready) {
// make previous call is resolved
sockRPC.onready();
sockRPC.onready = null;
}
if (typeof WebSocket == "undefined") { if (typeof WebSocket == "undefined") {
alerts.addAlert('Web sockets are not supported! Falling back to JSONP.', 'info'); alerts.addAlert('Web sockets are not supported! Falling back to JSONP.', 'info');
onready();
return; return;
} }
sockRPC.conf = conf || sockRPC.conf; sockRPC.conf = conf || sockRPC.conf;
@ -108,11 +127,13 @@ function(_, JSON, name, utils, alerts) {
sockRPC.sock.onclose = sockRPC.onclose; sockRPC.sock.onclose = sockRPC.onclose;
sockRPC.sock.onerror = sockRPC.onerror; sockRPC.sock.onerror = sockRPC.onerror;
sockRPC.sock.onmessage = sockRPC.onmessage; sockRPC.sock.onmessage = sockRPC.onmessage;
sockRPC.onready = onready;
} }
catch (ex) { catch (ex) {
// ignoring IE securty exception on local ip addresses // ignoring IE security exception on local ip addresses
console.log('not using websocket for aria2 rpc due to: ', ex); console.log('not using websocket for aria2 rpc due to: ', ex);
alerts.addAlert('Web sockets not working due to ' + ex.message, 'info'); alerts.addAlert('Web sockets not working due to ' + ex.message, 'info');
onready();
} }
}, },
}; };

View File

@ -6,6 +6,7 @@ angular
.factory('$syscall', ['$log', '$jsoncall', '$sockcall', '$alerts', .factory('$syscall', ['$log', '$jsoncall', '$sockcall', '$alerts',
function(log, jsonRPC, sockRPC, alerts) { function(log, jsonRPC, sockRPC, alerts) {
return { return {
state: 'none',
// called to initialize the rpc interface, call everytime configuration changes // called to initialize the rpc interface, call everytime configuration changes
// conf has the following structure: // conf has the following structure:
// { // {
@ -18,8 +19,14 @@ function(log, jsonRPC, sockRPC, alerts) {
// pass (string): password for the http authentication if enabled // pass (string): password for the http authentication if enabled
// } // }
init: function(conf) { init: function(conf) {
console.log("Syscall is initializing to", conf);
this.state = 'initializing';
jsonRPC.init(conf); jsonRPC.init(conf);
sockRPC.init(conf); var syscall = this;
sockRPC.init(conf, function() {
console.log("Syscall is ready");
syscall.state = 'ready';
});
}, },
// call this to start an rpc call, opts has the following structure: // call this to start an rpc call, opts has the following structure: