From 10727359bef4012ad34f656eeb14a4ac2f40ee3b Mon Sep 17 00:00:00 2001 From: slnpacifist Date: Wed, 13 Apr 2016 17:23:19 +0800 Subject: [PATCH] Added transport initialization phase. Websocket transport is not skipped anymore if jsonp is not able to connect. --- js/services/rpc/rpc.js | 17 +++++++++++++++-- js/services/rpc/sockcall.js | 25 +++++++++++++++++++++++-- js/services/rpc/syscall.js | 9 ++++++++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/js/services/rpc/rpc.js b/js/services/rpc/rpc.js index df40545..6d7792c 100644 --- a/js/services/rpc/rpc.js +++ b/js/services/rpc/rpc.js @@ -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 // only one syscall at max is ongoing // (i.e. serially) so should be private @@ -57,11 +60,18 @@ function(syscall, globalTimeout, alerts, utils, rootScope, uri, authconf) { }); var subs = subscriptions.slice(); if (!subs.length) { - timeout = setTimeout(update, time); + timeout = setTimeout(update, globalTimeout); 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]; if (currentConf && currentConf.auth && currentConf.auth.token) { currentToken = currentConf.auth.token; @@ -70,6 +80,8 @@ function(syscall, globalTimeout, alerts, utils, rootScope, uri, authconf) { currentToken = null; } syscall.init(currentConf); + timeout = setTimeout(update, globalTimeout); + return; } var params = _.map(subs, function(s) { @@ -84,6 +96,7 @@ function(syscall, globalTimeout, alerts, utils, rootScope, uri, authconf) { }); var error = function() { + needNewConnection = true; var ind = configurations.indexOf(currentConf); if (ind != -1) configurations.splice(ind, 1); diff --git a/js/services/rpc/sockcall.js b/js/services/rpc/sockcall.js index 009224f..4468c9b 100644 --- a/js/services/rpc/sockcall.js +++ b/js/services/rpc/sockcall.js @@ -28,17 +28,29 @@ function(_, JSON, name, utils, alerts) { _.each(sockRPC.handles, function(h) { h.error() }); sockRPC.handles = []; sockRPC.initialized = false; + if (sockRPC.onready) { + sockRPC.onready(); + sockRPC.onready = null; + } }, onclose: function(ev) { if (sockRPC.handles && sockRPC.handles.length) sockRPC.onerror('Connection reset while calling aria2'); sockRPC.initialized = false; + if (sockRPC.onready) { + sockRPC.onready(); + sockRPC.onready = null; + } }, // when connection opens onopen: function() { console.log('websocket initialized!!!'); 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 - init: function(conf) { + // onready is called when initial connection is resolved + init: function(conf, onready) { sockRPC.initialized = false; + if (sockRPC.onready) { + // make previous call is resolved + sockRPC.onready(); + sockRPC.onready = null; + } if (typeof WebSocket == "undefined") { alerts.addAlert('Web sockets are not supported! Falling back to JSONP.', 'info'); + onready(); return; } sockRPC.conf = conf || sockRPC.conf; @@ -108,11 +127,13 @@ function(_, JSON, name, utils, alerts) { sockRPC.sock.onclose = sockRPC.onclose; sockRPC.sock.onerror = sockRPC.onerror; sockRPC.sock.onmessage = sockRPC.onmessage; + sockRPC.onready = onready; } 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); alerts.addAlert('Web sockets not working due to ' + ex.message, 'info'); + onready(); } }, }; diff --git a/js/services/rpc/syscall.js b/js/services/rpc/syscall.js index a7a6eca..ebfaa60 100644 --- a/js/services/rpc/syscall.js +++ b/js/services/rpc/syscall.js @@ -6,6 +6,7 @@ angular .factory('$syscall', ['$log', '$jsoncall', '$sockcall', '$alerts', function(log, jsonRPC, sockRPC, alerts) { return { + state: 'none', // called to initialize the rpc interface, call everytime configuration changes // conf has the following structure: // { @@ -18,8 +19,14 @@ function(log, jsonRPC, sockRPC, alerts) { // pass (string): password for the http authentication if enabled // } init: function(conf) { + console.log("Syscall is initializing to", conf); + this.state = 'initializing'; 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: