made websocket reconnecting

This commit is contained in:
iou1name 2018-09-10 15:08:14 -04:00
parent b4cb801bcb
commit 9921cd7745

View File

@ -13,6 +13,26 @@ socket.onmessage = function(e) {
if (socket.events[event] === undefined) { return; } if (socket.events[event] === undefined) { return; }
socket.events[event](data); socket.events[event](data);
} }
socket.onclose = async function(e) {
console.log('WebSocket lost connection to server. Re-trying...');
let old_socket = socket;
while (true) {
await sleep(5000);
try {
socket = new WebSocket(this.url);
break;
}
catch (error) {
console.log('WebSocket lost connection to server. Re-trying...');
// this still throws an error in the console for some reason
}
}
socket.oldSend = old_socket.oldSend;
socket.send = old_socket.send;
socket.events = old_socket.events;
socket.onmessage = old_socket.onmessage;
socket.onclose = old_socket.onclose;
}
function load() { function load() {
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight; document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
let mtarea = document.getElementById('messageTextArea'); let mtarea = document.getElementById('messageTextArea');
@ -164,6 +184,9 @@ function strftime(date) {
function sort_by_votes(a, b) { function sort_by_votes(a, b) {
return b.cells[2].textContent.localeCompare(a.cells[2].textContent); return b.cells[2].textContent.localeCompare(a.cells[2].textContent);
} }
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/* DOM editing */ /* DOM editing */
function quote(message_id) { function quote(message_id) {