2019-02-15 10:01:33 -05:00
|
|
|
function load() {
|
2019-09-01 16:48:45 -04:00
|
|
|
//let intervalID = window.setInterval(get_active_torrents_ajax, 20000);
|
2019-09-12 21:05:25 -04:00
|
|
|
let intervalID = window.setInterval(update, 5000);
|
2019-02-15 10:01:33 -05:00
|
|
|
}
|
|
|
|
|
2019-09-01 16:48:45 -04:00
|
|
|
function get_active_torrents_ajax() {
|
2019-02-15 10:01:33 -05:00
|
|
|
let httpRequest;
|
|
|
|
httpRequest = new XMLHttpRequest();
|
|
|
|
httpRequest.onreadystatechange = function() {
|
|
|
|
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
|
|
|
|
if (httpRequest.status !== 200) { return; }
|
|
|
|
torrents = JSON.parse(httpRequest.responseText);
|
|
|
|
let html_str = '';
|
|
|
|
for (let i = 0; i < torrents.length; i++) {
|
2019-02-18 15:54:49 -05:00
|
|
|
html_str += '<tr>'
|
|
|
|
html_str += '<td class="name">' + torrents[i].name + '</td>';
|
2019-09-01 16:48:45 -04:00
|
|
|
html_str += '<td class="total_size_str">' + torrents[i].total_size_str + '</td>';
|
2019-02-18 15:54:49 -05:00
|
|
|
html_str += '<td class="state">' + torrents[i].state + '</td>';
|
2019-09-01 16:48:45 -04:00
|
|
|
html_str += '<td class="downrate_str">' + torrents[i].downrate_str + '</td>';
|
|
|
|
html_str += '<td class="down_percent">' + torrents[i].down_percent + '%</td>';
|
|
|
|
html_str += '<td class="eta_str">' + torrents[i].eta_str + '</td>';
|
|
|
|
html_str += '<td class="uprate_str">' + torrents[i].uprate_str + '</td>';
|
2019-02-18 15:54:49 -05:00
|
|
|
html_str += '<td class="tracker">' + torrents[i].tracker + '</td>';
|
2019-09-01 16:48:45 -04:00
|
|
|
html_str += '<td class="rtorrent_id">' + torrents[i].rtorrent_id + '</td>';
|
2019-02-18 15:54:49 -05:00
|
|
|
html_str += '</tr>';
|
2019-02-15 10:01:33 -05:00
|
|
|
}
|
|
|
|
document.getElementById('torrents').children[1].innerHTML = html_str;
|
|
|
|
};
|
|
|
|
httpRequest.open('GET', get_torrents_uri, true);
|
|
|
|
httpRequest.send();
|
|
|
|
}
|
2019-09-01 16:48:45 -04:00
|
|
|
|
|
|
|
var socket = new WebSocket('wss://' + window.location.hostname + ws_uri);
|
|
|
|
socket.oldSend = socket.send;
|
|
|
|
socket.send = function(event_title, data) {
|
|
|
|
data = JSON.stringify({event: event_title, data: data});
|
|
|
|
socket.oldSend.apply(this, [data]);
|
|
|
|
}
|
|
|
|
socket.events = {};
|
|
|
|
socket.onmessage = function(e) {
|
|
|
|
let data = JSON.parse(e.data);
|
|
|
|
let event = data.event;
|
|
|
|
data = data.data;
|
|
|
|
if (socket.events[event] === undefined) { return; }
|
|
|
|
socket.events[event](data);
|
|
|
|
}
|
|
|
|
socket.onclose = function(e) {
|
|
|
|
console.log('WebSocket lost connection to server. Re-trying...');
|
|
|
|
// TODO: reconnect
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Websocket receive */
|
|
|
|
socket.events['active_torrents'] = function(data) {
|
|
|
|
let table = document.querySelector('#torrents tbody');
|
|
|
|
while (table.firstChild) {
|
|
|
|
table.removeChild(table.firstChild);
|
|
|
|
}
|
|
|
|
data.forEach(function(torrent) {
|
|
|
|
let template = document.querySelector('#torrent_template');
|
|
|
|
let node = document.importNode(template.content, true);
|
|
|
|
for (let field of node.children[0].children) {
|
|
|
|
field.textContent = torrent[field.className];
|
|
|
|
}
|
|
|
|
table.appendChild(node);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
socket.events['tracker_stats'] = function(data) {
|
2019-09-12 21:05:25 -04:00
|
|
|
let table = document.querySelector('#tracker_stats tbody');
|
|
|
|
while (table.firstChild) {
|
|
|
|
table.removeChild(table.firstChild);
|
|
|
|
}
|
|
|
|
for (let [tracker, values] of Object.entries(data)) {
|
|
|
|
let template = document.querySelector('#tracker_template');
|
|
|
|
let node = document.importNode(template.content, true);
|
|
|
|
node.children[0].children[0].textContent = tracker;
|
|
|
|
for (let i = 0; i < values.length; i++) {
|
|
|
|
node.children[0].children[i+1].textContent = values[i];
|
|
|
|
}
|
|
|
|
table.appendChild(node);
|
|
|
|
}
|
2019-09-01 16:48:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Websocket send */
|
|
|
|
function get_active_torrents() {
|
|
|
|
socket.send('active_torrents', {});
|
|
|
|
}
|
|
|
|
function get_tracker_stats() {
|
|
|
|
socket.send('tracker_stats', {});
|
|
|
|
}
|
2019-09-12 21:05:25 -04:00
|
|
|
|
|
|
|
/* Helper */
|
|
|
|
function update() {
|
|
|
|
get_active_torrents();
|
|
|
|
get_tracker_stats();
|
|
|
|
}
|