From 9921cd7745ea349793f630060d3d3c8ba9c2ce78 Mon Sep 17 00:00:00 2001 From: iou1name Date: Mon, 10 Sep 2018 15:08:14 -0400 Subject: [PATCH] made websocket reconnecting --- quest/static/quest.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/quest/static/quest.js b/quest/static/quest.js index b7dfcae..aaf30c7 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -13,6 +13,26 @@ socket.onmessage = function(e) { if (socket.events[event] === undefined) { return; } 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() { document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight; let mtarea = document.getElementById('messageTextArea'); @@ -164,6 +184,9 @@ function strftime(date) { function sort_by_votes(a, b) { return b.cells[2].textContent.localeCompare(a.cells[2].textContent); } +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} /* DOM editing */ function quote(message_id) {