Fix the problem that aria2 status is incorrect sometimes

This commit is contained in:
MaysWind 2019-04-16 23:46:01 +08:00
parent 690345ccf8
commit de5884e94e
5 changed files with 92 additions and 6 deletions

View file

@ -285,12 +285,16 @@
});
});
aria2TaskService.onOperationSuccess(function () {
$rootScope.taskContext.rpcStatus = 'Connected';
aria2TaskService.onConnectionSuccess(function () {
if ($rootScope.taskContext.rpcStatus !== 'Connected') {
$rootScope.taskContext.rpcStatus = 'Connected';
}
});
aria2TaskService.onOperationError(function () {
$rootScope.taskContext.rpcStatus = 'Disconnected';
aria2TaskService.onConnectionFailed(function () {
if ($rootScope.taskContext.rpcStatus !== 'Disconnected') {
$rootScope.taskContext.rpcStatus = 'Disconnected';
}
});
aria2TaskService.onTaskCompleted(function (event) {

View file

@ -77,6 +77,13 @@
return;
}
if (context.connectionSuccessCallback) {
context.connectionSuccessCallback({
rpcUrl: rpcUrl,
method: method
});
}
if (context.successCallback) {
context.successCallback(data.id, data.result);
}
@ -94,6 +101,13 @@
innerError: true
}
};
if (context.connectionFailedCallback) {
context.connectionFailedCallback({
rpcUrl: rpcUrl,
method: method
});
}
}
if (context.errorCallback) {

View file

@ -9,6 +9,8 @@
var onFirstSuccessCallbacks = [];
var onOperationSuccessCallbacks = [];
var onOperationErrorCallbacks = [];
var onConnectionSuccessCallbacks = [];
var onConnectionFailedCallbacks = [];
var onDownloadStartCallbacks = [];
var onDownloadPauseCallbacks = [];
var onDownloadStopCallbacks = [];
@ -45,6 +47,8 @@
var invokeContext = {
uniqueId: uniqueId,
requestBody: requestBody,
connectionSuccessCallback: requestContext.connectionSuccessCallback,
connectionFailedCallback: requestContext.connectionFailedCallback,
successCallback: requestContext.successCallback,
errorCallback: requestContext.errorCallback
};
@ -132,6 +136,14 @@
methodName: (!isSystemMethod ? getAria2MethodFullName(methodName) : methodName)
};
context.connectionSuccessCallback = function () {
fireCustomEvent(onConnectionSuccessCallbacks);
};
context.connectionFailedCallback = function () {
fireCustomEvent(onConnectionFailedCallbacks);
};
if (secret && !isSystemMethod) {
finalParams.push(aria2RpcConstants.rpcTokenPrefix + secret);
}
@ -478,6 +490,12 @@
onOperationError: function (context) {
onOperationErrorCallbacks.push(context.callback);
},
onConnectionSuccess: function (context) {
onConnectionSuccessCallbacks.push(context.callback);
},
onConnectionFailed: function (context) {
onConnectionFailedCallbacks.push(context.callback);
},
onDownloadStart: function (context) {
onDownloadStartCallbacks.push(context.callback);
},

View file

@ -844,6 +844,28 @@
callback: callback
});
},
onConnectionSuccess: function (callback) {
if (!callback) {
ariaNgLogService.warn('[aria2TaskService.onConnectionSuccess] callback is null');
return;
}
aria2RpcService.onConnectionSuccess({
callback: callback
});
},
onConnectionFailed: function (callback) {
if (!callback) {
ariaNgLogService.warn('[aria2TaskService.onConnectionFailed] callback is null');
return;
}
aria2RpcService.onConnectionFailed({
callback: callback
});
},
onFirstSuccess: function (callback) {
if (!callback) {
ariaNgLogService.warn('[aria2TaskService.onFirstSuccess] callback is null');

View file

@ -28,6 +28,12 @@
context: context
});
if (content.result && context.connectionSuccessCallback) {
context.connectionSuccessCallback({
rpcUrl: rpcUrl
});
}
if (content.result && context.successCallback) {
ariaNgLogService.debug('[aria2WebSocketRpcService.request] ' + (context && context.requestBody && context.requestBody.method ? context.requestBody.method + ' ' : '') + 'response success', content);
@ -63,7 +69,7 @@
}
};
var getSocketClient = function () {
var getSocketClient = function (context) {
if (socketClient === null) {
try {
socketClient = $websocket(rpcUrl);
@ -85,6 +91,26 @@
processEventCallback(content);
}
});
socketClient.onOpen(function (e) {
ariaNgLogService.debug('[aria2WebSocketRpcService.onOpen] websocket is opened', e);
if (context && context.connectionSuccessCallback) {
context.connectionSuccessCallback({
rpcUrl: rpcUrl
});
}
});
socketClient.onClose(function (e) {
ariaNgLogService.warn('[aria2WebSocketRpcService.onClose] websocket is closed', e);
if (context && context.connectionFailedCallback) {
context.connectionFailedCallback({
rpcUrl: rpcUrl
});
}
});
} catch (ex) {
return {
success: false,
@ -106,7 +132,9 @@
return;
}
var client = getSocketClient();
var client = getSocketClient({
connectionFailedCallback: context.connectionFailedCallback
});
var uniqueId = context.uniqueId;
var requestBody = angular.toJson(context.requestBody);