freifunkmanager/webroot/js/socket.js

58 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-05-09 02:12:10 +02:00
var socket = {readyState:0};
2017-05-08 19:13:29 +02:00
(function(){
function onerror(err) {
console.warn(err);
2017-05-09 02:12:10 +02:00
if(socket.readyState !== 3){
notify.send("error","Es gibt Übertragungsprobleme!");
2017-05-09 02:12:10 +02:00
gui.render();
}
}
function onopen() {
2017-05-09 02:12:10 +02:00
gui.render();
}
2017-05-08 19:13:29 +02:00
function onmessage(e) {
2017-05-08 19:13:29 +02:00
var msg = JSON.parse(e.data);
if(msg.type === "current") {
2017-05-08 19:13:29 +02:00
store.updateReal(msg.node);
} else if (msg.type === "to-update") {
2017-05-08 19:13:29 +02:00
store.update(msg.node);
} else {
gui.disable(e);
}
2017-05-09 02:12:10 +02:00
gui.render();
}
2017-05-08 19:13:29 +02:00
function onclose(){
console.log("socket closed by server");
notify.send("warn","Es besteht ein Verbindungsproblem!");
2017-05-09 02:12:10 +02:00
gui.render();
setTimeout(connect, 5000);
}
function sendnode(node) {
2017-05-08 19:13:29 +02:00
var msg = {node:node};
var string = JSON.stringify(msg);
2017-05-09 02:12:10 +02:00
if(socket.send(string)){
notify.send("success","Node '"+node.node_id+"' mit neuen Werten wurde gespeichert.");
2017-05-09 02:12:10 +02:00
}else{
notify.send("error","Node '"+node.node_id+"' konnte nicht gespeichert werden!");
2017-05-09 02:12:10 +02:00
}
}
function connect() {
socket = new WebSocket(config.backend);
socket.onopen = onopen;
socket.onerror = onerror;
socket.onmessage = onmessage;
socket.onclose = onclose;
socket.sendnode = sendnode;
}
connect();
2017-05-08 19:13:29 +02:00
})();