support requesting rpc service using http protocol

This commit is contained in:
MaysWind 2016-05-16 21:28:23 +08:00
parent 38469b607d
commit 671124e3da
6 changed files with 52 additions and 13 deletions

View file

@ -185,6 +185,7 @@
<script src="scripts/filters/volumn.js"></script> <script src="scripts/filters/volumn.js"></script>
<script src="scripts/langs/en-US.js"></script> <script src="scripts/langs/en-US.js"></script>
<script src="scripts/langs/zh-CN.js"></script> <script src="scripts/langs/zh-CN.js"></script>
<script src="scripts/services/aria2HttpRpcService.js"></script>
<script src="scripts/services/aria2RpcService.js"></script> <script src="scripts/services/aria2RpcService.js"></script>
<script src="scripts/services/aria2WebSocketRpcService.js"></script> <script src="scripts/services/aria2WebSocketRpcService.js"></script>
<script src="scripts/services/ariaNgSettingService.js"></script> <script src="scripts/services/ariaNgSettingService.js"></script>

View file

@ -6,6 +6,7 @@
appPrefix: 'AriaNg' appPrefix: 'AriaNg'
}).constant('ariaNgDefaultOptions', { }).constant('ariaNgDefaultOptions', {
localeName: 'en-US', localeName: 'en-US',
protocol: 'http',
globalStatRefreshInterval: 1000, globalStatRefreshInterval: 1000,
downloadTaskRefreshInterval: 1000 downloadTaskRefreshInterval: 1000
}).constant('aria2RpcConstants', { }).constant('aria2RpcConstants', {

View file

@ -0,0 +1,33 @@
(function () {
'use strict';
angular.module('ariaNg').factory('aria2HttpRpcService', ['$http', 'ariaNgSettingService', function ($http, ariaNgSettingService) {
var rpcUrl = ariaNgSettingService.getJsonRpcUrl('http');
return {
request: function (context) {
if (!context) {
return;
}
var requestContext = {
url: rpcUrl,
method: 'POST',
data: context.requestBody
};
return $http(requestContext).success(function (data, header, config, status) {
if (!data || !data.result) {
return;
}
if (context.callback) {
context.callback(data.result);
}
}).error(function (data, header, config, status) {
//Do Nothing
});
}
};
}]);
})();

View file

@ -1,7 +1,7 @@
(function () { (function () {
'use strict'; 'use strict';
angular.module('ariaNg').factory('aria2RpcService', ['aria2RpcConstants', 'aria2WebSocketRpcService', 'utils', function (aria2RpcConstants, aria2WebSocketRpcService, utils) { angular.module('ariaNg').factory('aria2RpcService', ['aria2RpcConstants', 'ariaNgSettingService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', 'utils', function (aria2RpcConstants, ariaNgSettingService, aria2HttpRpcService, aria2WebSocketRpcService, utils) {
var invoke = function (method, context) { var invoke = function (method, context) {
context.uniqueId = utils.generateUniqueId(); context.uniqueId = utils.generateUniqueId();
context.requestBody = { context.requestBody = {
@ -11,7 +11,11 @@
params: context.params params: context.params
}; };
return aria2WebSocketRpcService.request(context); if (ariaNgSettingService.getProtocol() == 'ws') {
return aria2WebSocketRpcService.request(context);
} else {
return aria2HttpRpcService.request(context);
}
}; };
return { return {

View file

@ -2,7 +2,7 @@
'use strict'; 'use strict';
angular.module('ariaNg').factory('aria2WebSocketRpcService', ['$websocket', 'ariaNgSettingService', function ($websocket, ariaNgSettingService) { angular.module('ariaNg').factory('aria2WebSocketRpcService', ['$websocket', 'ariaNgSettingService', function ($websocket, ariaNgSettingService) {
var rpcUrl = ariaNgSettingService.getJsonRpcUrl(); var rpcUrl = ariaNgSettingService.getJsonRpcUrl('ws');
var socketClient = $websocket(rpcUrl); var socketClient = $websocket(rpcUrl);
var sendIdMapping = {}; var sendIdMapping = {};

View file

@ -29,12 +29,6 @@
}; };
return { return {
get: function (key) {
return getOption(key);
},
set: function (key, value) {
return setOption(key, value);
},
getLocaleName: function () { getLocaleName: function () {
return getOption('localeName'); return getOption('localeName');
}, },
@ -42,14 +36,20 @@
setOption('localeName', value); setOption('localeName', value);
$translate.use(value); $translate.use(value);
}, },
getJsonRpcUrl: function () { getJsonRpcUrl: function (protocol) {
var rpcHost = getOption('aria2RpcHost'); var rpcHost = getOption('aria2RpcHost');
if (!rpcHost) { if (!rpcHost) {
rpcHost = $location.$$host + ':6800'; rpcHost = $location.$$host + ':6800';
} }
return 'ws://' + rpcHost + '/jsonrpc'; return protocol + '://' + rpcHost + '/jsonrpc';
},
getProtocol: function () {
return getOption('protocol');
},
setProtocol: function (value) {
setOption('protocol', value);
}, },
getGlobalStatRefreshInterval: function () { getGlobalStatRefreshInterval: function () {
return getOption('globalStatRefreshInterval'); return getOption('globalStatRefreshInterval');