This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/src/scripts/services/aria2WebSocketRpcService.js

148 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2016-11-06 15:41:28 +01:00
angular.module('ariaNg').factory('aria2WebSocketRpcService', ['$q', '$websocket', 'ariaNgSettingService', 'ariaNgLogService', function ($q, $websocket, ariaNgSettingService, ariaNgLogService) {
2017-03-19 16:36:14 +01:00
var rpcUrl = ariaNgSettingService.getCurrentRpcUrl();
var socketClient = null;
2016-06-28 18:18:17 +02:00
2016-05-26 18:29:05 +02:00
var sendIdStates = {};
2016-06-28 18:18:17 +02:00
var eventCallbacks = {};
var processMethodCallback = function (content) {
var uniqueId = content.id;
if (!uniqueId) {
return;
}
var state = sendIdStates[uniqueId];
if (!state) {
return;
}
var context = state.context;
state.deferred.resolve({
success: true,
context: context
});
if (content.result && context.successCallback) {
ariaNgLogService.debug('[aria2WebSocketRpcService.request] response success', content);
2016-11-06 15:41:28 +01:00
2016-06-28 18:18:17 +02:00
context.successCallback(context.id, content.result);
}
if (content.error && context.errorCallback) {
2016-12-10 18:01:28 +01:00
ariaNgLogService.debug('[aria2WebSocketRpcService.request] response error', content);
2016-11-06 15:41:28 +01:00
2016-06-28 18:18:17 +02:00
context.errorCallback(context.id, content.error);
}
delete sendIdStates[uniqueId];
};
var processEventCallback = function (content) {
var method = content.method;
if (!method) {
return;
}
var callbacks = eventCallbacks[method];
if (!angular.isArray(callbacks) || callbacks.length < 1) {
return;
}
for (var i = 0; i < callbacks.length; i++) {
var callback = callbacks[i];
var context = (angular.isArray(content.params) && content.params.length > 0 ? content.params[0] : null);
callback(context);
}
};
2016-05-13 18:09:12 +02:00
var getSocketClient = function () {
2016-08-01 19:26:10 +02:00
if (socketClient === null) {
try {
socketClient = $websocket(rpcUrl);
socketClient.onMessage(function (message) {
if (!message || !message.data) {
return;
}
var content = angular.fromJson(message.data);
if (!content) {
return;
}
if (content.id) {
processMethodCallback(content);
} else if (content.method) {
processEventCallback(content);
}
});
} catch (ex) {
return {
success: false,
error: 'Cannot initialize WebSocket!',
exception: ex
}
}
2016-05-13 18:09:12 +02:00
}
return {
success: true,
instance: socketClient
};
};
2016-05-13 18:09:12 +02:00
return {
request: function (context) {
if (!context) {
return;
}
var client = getSocketClient();
2016-05-13 18:09:12 +02:00
var uniqueId = context.uniqueId;
var requestBody = angular.toJson(context.requestBody);
2016-12-10 18:01:28 +01:00
ariaNgLogService.debug('[aria2WebSocketRpcService.request] request start', context);
2016-11-06 15:41:28 +01:00
2016-05-26 18:29:05 +02:00
var deferred = $q.defer();
sendIdStates[uniqueId] = {
context: context,
deferred: deferred
};
if (client.instance) {
client.instance.send(requestBody);
} else {
deferred.reject({
success: false,
context: context
});
ariaNgLogService.debug('[aria2WebSocketRpcService.request] client error', client);
context.errorCallback(context.id, { message: client.error });
}
2016-05-13 18:09:12 +02:00
2016-05-26 18:29:05 +02:00
return deferred.promise;
2016-06-28 18:18:17 +02:00
},
on: function (eventName, callback) {
var callbacks = eventCallbacks[eventName];
if (!angular.isArray(callbacks)) {
callbacks = eventCallbacks[eventName] = [];
}
callbacks.push(callback);
2016-05-13 18:09:12 +02:00
}
};
}]);
2016-08-01 16:49:16 +02:00
}());