Skip to content

Commit

Permalink
feat: Add WebSocket Support
Browse files Browse the repository at this point in the history
  • Loading branch information
RossWang committed Nov 17, 2017
1 parent 3dd9e01 commit 3ed829f
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 69 deletions.
7 changes: 6 additions & 1 deletion App/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@
"description": ""
},

"OP_secure": {
"message": "Secure",
"description": ""
},

"OPN_protocol": {
"message": "RPC connection protocol of Aria2. Now you can only use http, https",
"message": "RPC connection protocol of Aria2.",
"description": ""
},

Expand Down
7 changes: 6 additions & 1 deletion App/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@
"description": ""
},

"OP_secure": {
"message": "安全",
"description": ""
},

"OPN_protocol": {
"message": "Aria2的RPC通讯协议。现在你只能用 http, https。",
"message": "Aria2的RPC通讯协议。",
"description": ""
},

Expand Down
2 changes: 1 addition & 1 deletion App/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
},

"OPN_protocol": {
"message": "Aria2的RPC通訊協定。現在你只能用 http, https。",
"message": "Aria2的RPC通訊協定。",
"description": ""
},

Expand Down
167 changes: 105 additions & 62 deletions App/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,52 @@ function humanFileSize(bytes, si) {
return bytes.toFixed(1)+' '+units[u];
}

function isRunning(item) {
function isRunning(item, aria2) {
//check whether aria2 is runnning
var xhttp = new XMLHttpRequest();
var url = "aria2://"
if (item.shutdown)
url += "stop-with-process";
xhttp.ontimeout = function() {
if (item.auto) {
var creating = browser.windows.create({
url: url,
type: "popup",
width: 50,
height: 50,
});
creating.then(windowInfo => {
browser.windows.remove(windowInfo.id);
}, () => {});
}
};
xhttp.timeout = 400;
xhttp.open("GET", item.protocol + "://" + item.host + ":" + item.port +
"/", true);
xhttp.send();
if (item.protocol.toLowerCase() == "ws" || item.protocol.toLowerCase() == "wss") {
aria2.open().then(
function (res) {
aria2.close();
},
function (err) {
if (item.auto) {
var creating = browser.windows.create({
url: url,
type: "popup",
width: 50,
height: 50,
});
creating.then(windowInfo => {
browser.windows.remove(windowInfo.id);
}, () => {});
}
}
);
}
else {
aria2.getVersion().then(
function (res) {
console.log('result', res);
},
function (err) {
if (item.auto) {
var creating = browser.windows.create({
url: url,
type: "popup",
width: 50,
height: 50,
});
creating.then(windowInfo => {
browser.windows.remove(windowInfo.id);
}, () => {});
}
}
);
}
}

function sendTo(url, fileName, filePath, header) {
Expand All @@ -64,63 +87,83 @@ function sendTo(url, fileName, filePath, header) {
}
else {
browser.storage.local.get(config.command.guess, function(item) {
var secure = false;
if (item.protocol.toLowerCase == "https" || item.protocol.toLowerCase == "wss")
secure = true;
var options = {
host: item.host,
port: item.port,
secure: secure,
secret: item.token,
path: "/" + item.interf
};

var aria2 = new Aria2(options);
// check whether aria2 is runnning
isRunning(item);
isRunning(item, aria2);

// Send TO Aria2
var xhr = new XMLHttpRequest();
filePath = filePath.replace(/\\/g, '\\\\');
item.path = item.path.replace(/\\/g, '\\\\');
var params = {};
if (header != "[]")
params.header = header;
params.out = fileName;
if (filePath != "") {
// file path from download panel
var params =
`{"jsonrpc":"2.0","method":"aria2.addUri","id":"ext","params":["token:` +
item.token + `",["` + url + `"],{"header":` + header + `,"out":"` +
fileName + `","dir":"` + filePath + `"}]}`;
params.dir = filePath;
}
else if (item.path != "") {
// file path from setting
var params =
`{"jsonrpc":"2.0","method":"aria2.addUri","id":"ext","params":["token:` +
item.token + `",["` + url + `"],{"header":` + header + `,"out":"` +
fileName + `","dir":"` + item.path + `"}]}`;
params.dir = item.path;
}
if (item.protocol.toLowerCase() == "ws" || item.protocol.toLowerCase() == "wss") {
aria2.open().then(
function (res) {
aria2.addUri([url], params).then(
function (res) {
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
},
function (err) {
// retry again after 3 seconds
setTimeout( () => {aria2.addUri([url], params).then(
function (res) {
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
},
function (err) {
notify(browser.i18n.getMessage("error_connect"));
}
);}, 3000);
}
);
aria2.close();
},
function (err) {
console.log('error', err);
notify(browser.i18n.getMessage("error_connect"));
}
);
}
else {
// default file path
var params =
`{"jsonrpc":"2.0","method":"aria2.addUri","id":"ext","params":["token:` +
item.token + `",["` + url + `"],{"header":` + header + `,"out":"` +
fileName + `"}]}`;
aria2.addUri([url], params).then(
function (res) {
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
},
function (err) {
// retry again after 3 seconds
setTimeout( () => {aria2.addUri([url], params).then(
function (res) {
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
},
function (err) {
console.log('error', err);
notify(browser.i18n.getMessage("error_connect"));
}
);}, 3000);
}
);
}

console.log(params);

// callback
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
} else if (xhr.readyState == 4 && xhr.status != 0) {
notify(browser.i18n.getMessage("error_connect"));
}
};

// retry again after 5 seconds
xhr.onerror = function() {
setTimeout( () => {
xhr.open("POST", item.protocol + "://" + item.host + ":" + item.port +
"/" + item.interf, true);
xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
xhr.onerror = function() {notify(browser.i18n.getMessage("error_connect"));};
//console.log(xhr);
xhr.send(params)
}, 5000);
};

xhr.timeout = 4000;
xhr.open("POST", item.protocol + "://" + item.host + ":" + item.port +
"/" + item.interf, true);
xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
xhr.send(params);
console.log(url, params);
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion App/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ config.command = {
get guess() {
return {
path: "",
protocol: "http",
protocol: "ws",
host: "127.0.0.1",
port: "6800",
interf: "jsonrpc",
Expand Down
5 changes: 2 additions & 3 deletions App/data/options/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
body {
font-family: "Helvetica Neue",Helvetica,sans-serif;
font-size: 16px;
background: rgb(251,251,251);
background: #f9f9fa;
}
p {
margin: 3px;
Expand Down Expand Up @@ -72,8 +72,7 @@
</tr>
<tr>
<td></td>
<td data-message="OPN_protocol" class="note">RPC connection protocol of Aria2. Now you can only use
http, https</td>
<td data-message="OPN_protocol" class="note">RPC connection protocol of Aria2</td>
</tr>
<tr>
<td data-message="OP_host">Host:</td>
Expand Down
28 changes: 28 additions & 0 deletions App/lib/polygoat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
;(function (global) {
'use strict'

function polygoat (fn, cb, Promise) {
if (cb) {
fn(function (err, res) {
cb(err, res)
})
} else {
var P = Promise || global.Promise
return new P(function (resolve, reject) {
fn(function (err, res) {
if (err !== null && err !== undefined) {
reject(err)
} else {
resolve(res)
}
})
})
}
}

if (typeof module !== 'undefined' && module.exports) {
module.exports = polygoat
} else {
window.polygoat = polygoat
}
}(typeof global !== 'undefined' ? global : this))

0 comments on commit 3ed829f

Please sign in to comment.