From 56f26bed1446453bc169b589207cb683ce31d26a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 24 Sep 2020 13:24:35 +0300 Subject: [PATCH] Minor cleanup of JavaScript code --- server/static/scripts.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/server/static/scripts.js b/server/static/scripts.js index d9d3395..9d7516b 100644 --- a/server/static/scripts.js +++ b/server/static/scripts.js @@ -22,8 +22,11 @@ function onLoad() { } function updateRooms() { + // Configure a different API URL here if desired + const api_url = '/api/rooms'; + var request = new XMLHttpRequest(); - request.open('GET', '/api/rooms', true); + request.open('GET', api_url, true); request.onload = function () { if (request.status >= 200 && request.status < 400) { var rooms = JSON.parse(this.response); @@ -140,19 +143,29 @@ function pauseRoom() { playing_room = null; } -function connectWebsocket() { +function getWebSocketUrl() { var scheme; + var port; + + // Configure a different WebSocket server here if desired if (window.location.protocol == 'https:') { scheme = 'wss:'; + port = 443; } else if (window.location.protocol == 'http:') { scheme = 'ws:'; + port = 80; } + const server = window.location.hostname; - const port = window.location.port || 80; + port = window.location.port || port; const ws_url = scheme + '//' + server + ':' + port + '/ws/subscribe'; - const ws = new WebSocket(ws_url); + return ws_url; +} + +function connectWebsocket() { + const ws = new WebSocket(getWebSocketUrl()); ws.addEventListener('open', (event) => onServerOpen(ws, event)); ws.addEventListener('error', (event) => onServerError(ws, event)); @@ -168,8 +181,6 @@ function onServerOpen(ws, event) { return; } - console.log('Opened'); - // Now join the room, at this point we will start getting messages websocket.send(JSON.stringify({ 'joinroom': { -- GitLab