/* House keeping */ var socket = new WebSocket('wss://' + document.domain + SCRIPT_NAME + '/ws/quest/' + quest_id + '/'); socket.oldSend = socket.send; socket.send = function(event_title, data) { data = JSON.stringify({event: event_title, data: data}); socket.oldSend.apply(this, [data]); } socket.events = {}; socket.onmessage = function(e) { let data = JSON.parse(e.data); let event = data.event; data = data.data; 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'); mtarea.addEventListener('keypress', function(event) { if (event.key === 'Enter' && !event.shiftKey) { let text = mtarea.value.trim(); mtarea.value = ''; if (text === '') { return; } socket.send('message', {message: text}); } }); live_countdown(); } /* Websocket receive */ socket.events['message'] = function(data) { let msg_str = '
'; post_str += ' | '; post_str += '' + data.options[i][1] + ' | '; post_str += '0 |