Refactor JS, handle WebSocket errors
This commit is contained in:
parent
0b65c79a89
commit
952b8940c9
|
@ -95,25 +95,51 @@
|
||||||
var queue = [];
|
var queue = [];
|
||||||
var archive = [];
|
var archive = [];
|
||||||
|
|
||||||
// Construct websocket url
|
function connectWebSocket(url) {
|
||||||
var loc = window.location;
|
var webSocket = new WebSocket(url);
|
||||||
var ws_url = ((loc.protocol === "https:") ? "wss://" : "ws://") + loc.host + "/ws";
|
|
||||||
|
|
||||||
var exampleSocket = new WebSocket(ws_url);
|
webSocket.onopen = function(event) {
|
||||||
exampleSocket.onopen = function (event) {
|
console.log("WebSocket: Connected");
|
||||||
console.log("WS: Connection open");
|
}
|
||||||
};
|
|
||||||
exampleSocket.onmessage = function (event) {
|
webSocket.onmessage = function(event) {
|
||||||
console.log("WS: Received message");
|
console.log("WebSocket: Received Message");
|
||||||
IT = JSON.parse(event.data);
|
handleMessage(event.data);
|
||||||
if (IT["State"] === 1) {
|
}
|
||||||
queue.push(IT);
|
|
||||||
archive.push(IT);
|
webSocket.onclose = function(event) {
|
||||||
if (archive.length > 5) {
|
console.log("WebSocket: Closed");
|
||||||
archive.shift();
|
setTimeout(function() {
|
||||||
}
|
connectWebSocket(url);
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
webSocket.onerror = function(event) {
|
||||||
|
console.warn("WebSocket: Error, closing socket");
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleMessage(message) {
|
||||||
|
var item = null;
|
||||||
|
try {
|
||||||
|
item = JSON.parse(message);
|
||||||
|
} catch (err) {
|
||||||
|
console.warn("MessageHandler: Failed Message JSON parsing");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (item["State"] === 1) {
|
||||||
|
addItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function addItem(item) {
|
||||||
|
queue.push(item);
|
||||||
|
archive.push(item);
|
||||||
|
if (archive.length > 5) {
|
||||||
|
archive.shift();
|
||||||
}
|
}
|
||||||
console.log(event.data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayNewSnap() {
|
function displayNewSnap() {
|
||||||
|
@ -180,7 +206,12 @@ function setDisplayItem(item) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Construct websocket url
|
||||||
|
var loc = window.location;
|
||||||
|
var ws_url = ((loc.protocol === "https:") ? "wss://" : "ws://") + loc.host + "/ws";
|
||||||
|
|
||||||
loadInitialSnaplist();
|
loadInitialSnaplist();
|
||||||
|
connectWebSocket(ws_url);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Reference in a new issue