Giter VIP home page Giter VIP logo

aria2.js's Introduction

Usage

Download

Latest

HTML

<script src="https://jc3213.github.io/aria2.js/aria2.js"></script>

TamperMonkey

// @require https://jc3213.github.io/aria2.js/aria2.js

Syntax

let aria2 = new Aria2("http://localhost:6800/jsonrpc", "mysecret");
let aria2 = new Aria2("http", "localhost:6800/jsonrpc", "mysecret"); // Requires 0.2.0~
let aria2 = new Aria2("http://localhost:6800/jsonrpc#mysecret"); // Requires 0.4.0~

Getter & Setter

scheme

console.log(aria2.scheme); // current scheme
aria2.scheme = scheme; // set new scheme
  • Requires 0.2.0~
  • scheme
    • http, https, ws, and wss

url

console.log(aria2.url); // current url
aria2.url = url; // set new url
  • Requires 0.3.0~
  • url
    • ${hostname}:${port}/jsonrpc
  • hostname
    • www.example.com
  • port
    • 6800 default
    • 443 for SSL

secret

console.log(aria2.secret); // current secret token
aria2.secret = secret; // set new secret token
  • Requires 0.3.0~
  • secret
    • string, secret token of aria2 json-rpc
    • returns ${secret}

onmessage

console.log(aria2.onmessage); // current message event listener
aria2.onmessage = callback; // set new message event listener
  • Requires 0.2.0~
  • Handle the event when WebSocket message is recieved
  • callback
    • function, (response) => void
    • returns ${callback}
    • Used for JSON-RPC over WebSocket notifications

onclose

console.log(aria2.onclose); // current message event listener
aria2.onclose = callback; // set new message event listener
  • Requires 0.5.0~
  • Handle the event when WebSocket connection is closed
  • callback
    • function, (event) => void
    • returns ${callback}
    • It will run when WebSocket connection is closed

Method

  • call
    • Use WebSocket or HTTP Post based on scheme
  • send
    • Use WebSocket method only
  • post
    • Use HTTP Post method only

call

let response = aria2.call( { method, params } );
let response = aria2.call( { method, params }, { method, params }, ..., { method, params } );
  • response
    • Promise object, return an array that contains the response from jsonrpc if fulfilled
  • method required
  • params optional
    • JSON-RPC method call parameters

Code Sample

let jsonrpc = {};
let session = {};
let retry;
let update;
let aria2 = new Aria2("http://localhost:6800/jsonrpc#mysecret");
aria2.onmessage = aria2WebsocketNotification;
aria2.onclose = aria2ClientInitiate;
aria2ClientInitiate();

function aria2ClientInitiate() {
    clearTimeout(retry);
    clearInterval(update);
    session.all = {};
    session.active = {};
    session.waiting = {};
    session.stopped = {};
    aria2.call(
        {method: 'aria2.getGlobalOption'},
        {method: 'aria2.getVersion'},
        {method: 'aria2.getGlobalStat'},
        {method: 'aria2.tellActive'},
        {method: 'aria2.tellWaiting', params: [0, 999]},
        {method: 'aria2.tellStopped', params: [0, 999]}
    ).then((response) => {
        let [global, version, stats, active, waiting, stopped] = response;
        jsonrpc.options = global.result;
        jsonrpc.version = version.result;
        jsonrpc.stat = stats.result;
        active.result.forEach((result) => session.active[result.gid] = session.all[result.gid] = result);
        waiting.result.forEach((result) => session.waiting[result.gid] = session.all[result.gid] = result);
        stopped.result.forEach((result) => session.stopped[result.gid] = session.all[result.gid] = result);
        update = setInterval(aria2UpdateStats, 10000);
    }).catch((error) => {
        retry = setTimeout(aria2JsonrpcInitiate, 5000);
    });
}

async function aria2UpdateStats() {
    let response = await aria2RPC.call({method: 'aria2.getGlobalStat'}, {method: 'aria2.tellActive'});
    let [stats, active] = response;
    jsonrpc.stat = stats.result;
    active.result.forEach((result) => session.active[result.gid] = session.all[result.gid] = result);
}

async function aria2PurgeDownload() {
    let response = await aria2RPC.call({method: 'aria2.purgeDownloadResult'});
    session.all = {...session.active, ...session.waiting};
    session.stopped = {};
    jsonrpc.stat['numStopped'] = '0';
}

async function aria2WebsocketNotification(response) {
    if (!response.method) { return; }
    let gid = response.params[0].gid;
    let res = await aria2.call({method: 'aria2.tellStatus', params: [gid]});
    let result = res[0].result;
    switch (response.method) {
        case 'aria2.onBtDownloadComplete':
            break;
        case 'aria2.onDownloadStart':
            console.log("The session #" + gid + " has started");
            if (session.waiting[gid]) {
                delete session.waiting[gid];
                session.active[gid] = result;
            }
            break;
        case 'aria2.onDownloadComplete':
            console.log("The session #" + gid + " has completed");
        default:
            if (session.active[gid]) {
                delete session.active[gid];
                switch (result.status) {
                    case 'waiting':
                    case 'paused':
                        session.waiting[gid] = result;
                        break;
                    case 'complete':
                    case 'removed':
                    case 'error':
                        session.stopped[gid] = result;
                        break;
                }
            }
            break;
    }
}

aria2.js's People

Contributors

jc3213 avatar

Stargazers

Hassan Yousefi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.