websocket support turned on

This commit is contained in:
hamza zia 2012-09-18 22:53:35 +08:00
parent 932a72d53f
commit e1675c513d
2 changed files with 870 additions and 870 deletions

1438
index.html

File diff suppressed because it is too large Load Diff

View File

@ -1,151 +1,151 @@
var JsonRPC = function(conf) { var JsonRPC = function(conf) {
if (!this instanceof JsonRPC) if (!this instanceof JsonRPC)
return new JsonRPC(); return new JsonRPC();
this.avgTimeout = 1000; this.avgTimeout = 1000;
this.serverConf = conf; this.serverConf = conf;
}; };
JsonRPC.prototype = { JsonRPC.prototype = {
encode: function(obj) { encode: function(obj) {
return base64.btoa(JSON.stringify(obj)); return base64.btoa(JSON.stringify(obj));
}, },
ariaRequest: function(url, multicall, funcName, params, success, error) { ariaRequest: function(url, multicall, funcName, params, success, error) {
var startTime = new Date(); var startTime = new Date();
$.ajax({ $.ajax({
url: url, url: url,
timeout: 1000, timeout: 1000,
data: { data: {
jsonrpc: 2.0, jsonrpc: 2.0,
id: 'webui', id: 'webui',
method: funcName.search(/\./g) == -1 ? 'aria2.' + funcName : funcName, method: funcName.search(/\./g) == -1 ? 'aria2.' + funcName : funcName,
params: params.length ? this.encode(params) : undefined params: params.length ? this.encode(params) : undefined
}, },
success: function(data) { success: function(data) {
this.avgTimeout = 3 * (new Date() - startTime); this.avgTimeout = 3 * (new Date() - startTime);
return success(data) return success(data)
}, },
error: error, error: error,
dataType: 'jsonp', dataType: 'jsonp',
jsonp: 'jsoncallback' jsonp: 'jsoncallback'
}); });
}, },
invoke: function(opts) { invoke: function(opts) {
var rpc = this; var rpc = this;
rpc.ariaRequest( rpc.ariaRequest(
'http://' + rpc.serverConf.host + ':' + rpc.serverConf.port + '/jsonrpc', 'http://' + rpc.serverConf.host + ':' + rpc.serverConf.port + '/jsonrpc',
opts.multicall, opts.multicall,
opts.func, opts.func,
opts.params, opts.params,
opts.success, opts.success,
function() { function() {
// check if authentication details are given, if yes then use a hack to support // check if authentication details are given, if yes then use a hack to support
// http authentication otherwise emit error // http authentication otherwise emit error
if (!rpc.serverConf.user.length) { if (!rpc.serverConf.user.length) {
console.log("no user name and still error!!!"); console.log("no user name and still error!!!");
return opts.error(); return opts.error();
} }
var authUrl = 'http://' + var authUrl = 'http://' +
rpc.serverConf.user + ":" + rpc.serverConf.user + ":" +
rpc.serverConf.pass + "@" + rpc.serverConf.pass + "@" +
rpc.serverConf.host + ':' + rpc.serverConf.host + ':' +
rpc.serverConf.port + '/jsonrpc'; rpc.serverConf.port + '/jsonrpc';
// hack is to basically inject an image with same uri as the aria2 rpc url, // hack is to basically inject an image with same uri as the aria2 rpc url,
// most browsers will then cache the authentication details and we dont have // most browsers will then cache the authentication details and we dont have
// to give them next time we make a request // to give them next time we make a request
var img = $('<img/>').attr("src", authUrl); var img = $('<img/>').attr("src", authUrl);
$('body').append(img); $('body').append(img);
img.remove(); img.remove();
// timeout to let the image load and then make a request, // timeout to let the image load and then make a request,
setTimeout(function() { setTimeout(function() {
rpc.ariaRequest( rpc.ariaRequest(
authUrl, authUrl,
opts.multicall, opts.multicall,
opts.params, opts.params,
opts.success, opts.success,
opts.error opts.error
); );
}, rpc.avgTimeout); }, rpc.avgTimeout);
} }
); );
} }
}; };
var SocketRPC = function(conf) { var SocketRPC = function(conf) {
if (!this instanceof SocketRPC) if (!this instanceof SocketRPC)
return new SocketRPC(); return new SocketRPC();
var rpc = this; var rpc = this;
rpc.serverConf = conf; rpc.serverConf = conf;
rpc.initialized = false; rpc.initialized = false;
rpc.handles = []; rpc.handles = [];
rpc.sock = new WebSocket('ws://' + conf.host + ':' + conf.port + '/jsonrpc'); rpc.sock = new WebSocket('ws://' + conf.host + ':' + conf.port + '/jsonrpc');
rpc.sock.onopen = function() { rpc.sock.onopen = function() {
rpc.initialized = true; rpc.initialized = true;
}; };
rpc.sock.onclose = rpc.sock.onerror = function() { rpc.sock.onclose = rpc.sock.onerror = function() {
_.each(handles, function(h) { h.error() }); _.each(handles, function(h) { h.error() });
rpc.handles = []; rpc.handles = [];
}; };
rpc.sock.onmessage = function(message) { rpc.sock.onmessage = function(message) {
var data = JSON.parse(message.data); var data = JSON.parse(message.data);
for (var i = rpc.handles.length; i--; ) { for (var i = rpc.handles.length; i--; ) {
if (rpc.handles[i].id === data.id) { if (rpc.handles[i].id === data.id) {
if (data.error) if (data.error)
rpc.handles[i].error(); rpc.handles[i].error();
else else
rpc.handles[i].success(data); rpc.handles[i].success(data);
rpc.handles.splice(i, 1); rpc.handles.splice(i, 1);
return; return;
} }
} }
}; };
} }
SocketRPC.prototype = { SocketRPC.prototype = {
invoke: function(opts) { invoke: function(opts) {
var data = { var data = {
jsonrpc: 2.0, jsonrpc: 2.0,
id: 'webui_' + utils.randStr(), id: 'webui_' + utils.randStr(),
method: opts.func.search(/\./g) == -1 ? 'aria2.' + opts.func : opts.func, method: opts.func.search(/\./g) == -1 ? 'aria2.' + opts.func : opts.func,
params: opts.params.length ? opts.params : undefined params: opts.params.length ? opts.params : undefined
}; };
this.handles.push({ this.handles.push({
success: opts.success, success: opts.success,
error: opts.error, error: opts.error,
id: data.id id: data.id
}); });
this.sock.send(JSON.stringify(data)); this.sock.send(JSON.stringify(data));
}, },
}; };
var AriaConnection = function(conf) { var AriaConnection = function(conf) {
var conf = conf || { var conf = conf || {
host: 'localhost', host: 'localhost',
port: 6800, port: 6800,
user: '', user: '',
pass: '' pass: ''
}; };
var jRPC = new JsonRPC(conf); var jRPC = new JsonRPC(conf);
if (typeof WebSocket != "undefined") { if (typeof WebSocket != "undefined") {
var sockRPC = new SocketRPC(conf); var sockRPC = new SocketRPC(conf);
} }
return { return {
conf: conf, conf: conf,
invoke: function(opts) { invoke: function(opts) {
opts = utils.mixin(opts, { opts = utils.mixin(opts, {
success: function() {}, success: function() {},
error: function() {}, error: function() {},
params: [], params: [],
func: '' func: ''
}); });
if (!sockRPC || !sockRPC.initialized || true) if (!sockRPC || !sockRPC.initialized)
return jRPC.invoke(opts); return jRPC.invoke(opts);
else else
return sockRPC.invoke(opts); return sockRPC.invoke(opts);
} }
}; };
} }