// started on main page load
function init() {
init_stats();
}
function set_status(message)
{
if (message == "") {
document.getElementById("status").textContent = message;
document.getElementById("status").style.display = 'inline';
} else {
document.getElementById("status").textContent = "";
document.getElementById("status").style.display = 'none';
}
}
var websocket;
//
// Callbacks for websocket data of different types
//
function on_stats(numberofdocuments, numberofconnections, dbsizegross, dbsizenet)
{
document.getElementById("numberofdocuments").textContent = numberofdocuments;
document.getElementById("numberofconnections").textContent = numberofconnections;
document.getElementById("dbsizegross").textContent = dbsizegross;
document.getElementById("dbsizenet").textContent = dbsizenet;
}
function on_version(version)
{
document.getElementById("version").textContent = version;
}
function on_message(e) {
var parser = new DOMParser();
var xmlDocument = parser.parseFromString(e.data, "text/xml");
var type = xmlDocument.getElementsByTagName("type")[0].textContent;
if (type == "stats") {
on_stats(xmlDocument.getElementsByTagName("numberofdocuments")[0].textContent,
xmlDocument.getElementsByTagName("numberofconnections")[0].textContent,
xmlDocument.getElementsByTagName("dbsizegross")[0].textContent,
xmlDocument.getElementsByTagName("dbsizenet")[0].textContent);
} else if (type == "version") {
on_version(xmlDocument.getElementsByTagName("version")[0].textContent);
} else if (type == "error") {
alert(xmlDocument.getElementsByTagName("message")[0].textContent);
} else {
alert("Unhandled message type: " + e.data + "|" + type);
}
}
function connect_websocket() {
document.getElementById("reconnect").style.display = 'none';
set_status("Connecting...");
var newlocation = location.origin + location.pathname;
newlocation = newlocation.replace(/^http/, 'ws');
newlocation = newlocation.replace(/stats.html$/, '');
if (newlocation.slice(-1) != "/")
newlocation += "/";
newlocation += "websocket";
websocket = new WebSocket(newlocation);
websocket.onmessage = function(e) { on_message(e); };
websocket.onopen = function(e) {
websocket.send("getversion");
websocket.send("getstats");
set_status(""); // ok
};
websocket.onclose = function(e) {
alert("Server connection closed.");
document.getElementById("reconnect").style.display = 'inline';
};
websocket.onerror = function(e) {
alert("Error: Server connection closed.");
document.getElementById("reconnect").style.display = 'inline';
};
}
// button in html
function on_reconnect_click() {
connect_websocket();
}
function init_stats() {
set_status("Loading...");
connect_websocket();
}