attempt client reconnection of closed websockets

This commit is contained in:
Annika Backstrom 2017-04-09 22:02:55 -04:00
parent 7e519c4736
commit abe3c9f8aa

View File

@ -1,18 +1,31 @@
window.onload = function() { window.onload = function() {
console.log('window.onload'); var play_log = document.getElementById('play-log');
var last_tag = document.getElementById('last-tag');
var play_log = document.getElementById('play-log'); function updateLog(data) {
var last_tag = document.getElementById('last-tag'); last_tag.innerHTML = data.tag;
play_log.innerHTML = data.html;
}
function updateLog(data) { var host = window.document.location.host.replace(/:.*/, '');
last_tag.innerHTML = data.tag; var ws;
play_log.innerHTML = data.html;
function connectWebsocket() {
console.log('Attempting WebSocket connection...');
ws = new WebSocket('ws://' + host + ':' + jukebox.port);
ws.addEventListener('open', function(event) {
console.log('WebSocket connection opened.');
});
ws.addEventListener('message', function(event) {
updateLog(JSON.parse(event.data));
});
}
setInterval(function() {
if (ws.readyState == WebSocket.CLOSED) {
connectWebsocket();
} }
}, 1000);
var host = window.document.location.host.replace(/:.*/, ''); connectWebsocket();
var ws = new WebSocket('ws://' + host + ':' + jukebox.port);
ws.onmessage = function (event) {
updateLog(JSON.parse(event.data));
};
}; };