Improve queue and websocket address

This commit is contained in:
Marvin Scholz 2017-08-18 08:48:41 +02:00
parent 6e482a2220
commit b68b824b06
2 changed files with 30 additions and 5 deletions

View file

@ -87,7 +87,10 @@ var app = new Vue({
}
})
var exampleSocket = new WebSocket("ws://127.0.0.1:8000/ws");
// Construct websocket url
var loc = window.location;
var ws_url = ((loc.protocol === "https:") ? "wss://" : "ws://") + loc.host + "/ws";
var exampleSocket = new WebSocket(ws_url);
exampleSocket.onopen = function (event) {
console.log("WS: Connection open!");
console.log("Proto: " + exampleSocket.protocol);

View file

@ -104,6 +104,7 @@ function connectWebSocket(url) {
webSocket.onmessage = function(event) {
console.log("WebSocket: Received Message");
console.log(event.data);
handleMessage(event.data);
}
@ -135,10 +136,31 @@ function handleMessage(message) {
}
function addItem(item) {
queue.push(item);
archive.push(item);
if (archive.length > 5) {
archive.shift();
var exists = 0;
for (var i in queue) {
if (queue.hasOwnProperty(i)) {
if (queue[i]["ID"] === item["ID"]) {
exists = 1;
}
}
}
if (exists === 0) {
queue.push(item);
}
exists = 0;
for (var i in archive) {
if (archive.hasOwnProperty(i)) {
if (archive[i]["ID"] === item["ID"]) {
exists = 1;
}
}
}
if (exists === 0) {
archive.push(item);
if (archive.length > 5) {
archive.shift();
}
}
}