diff --git a/src/scripts/core/root.js b/src/scripts/core/root.js index 39d1cef..ac1bcda 100644 --- a/src/scripts/core/root.js +++ b/src/scripts/core/root.js @@ -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) { diff --git a/src/scripts/services/aria2HttpRpcService.js b/src/scripts/services/aria2HttpRpcService.js index 6a96c8f..5607b80 100644 --- a/src/scripts/services/aria2HttpRpcService.js +++ b/src/scripts/services/aria2HttpRpcService.js @@ -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) { diff --git a/src/scripts/services/aria2RpcService.js b/src/scripts/services/aria2RpcService.js index 183741f..99cac20 100644 --- a/src/scripts/services/aria2RpcService.js +++ b/src/scripts/services/aria2RpcService.js @@ -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); }, diff --git a/src/scripts/services/aria2TaskService.js b/src/scripts/services/aria2TaskService.js index b264b38..73624b3 100644 --- a/src/scripts/services/aria2TaskService.js +++ b/src/scripts/services/aria2TaskService.js @@ -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'); diff --git a/src/scripts/services/aria2WebSocketRpcService.js b/src/scripts/services/aria2WebSocketRpcService.js index 09e3152..29cfb22 100644 --- a/src/scripts/services/aria2WebSocketRpcService.js +++ b/src/scripts/services/aria2WebSocketRpcService.js @@ -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);