From fc8d3d8f11e2e8d937d699c650ca60dc55786ecc Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Tue, 3 Dec 2019 18:53:57 +0100 Subject: [PATCH] improve websocket url determination --- htdocs/map.js | 22 +++++++++++++--------- htdocs/openwebrx.js | 24 +++++++++++++----------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/htdocs/map.js b/htdocs/map.js index 0089f47..c912a14 100644 --- a/htdocs/map.js +++ b/htdocs/map.js @@ -1,9 +1,4 @@ (function(){ - var protocol = 'ws'; - if (window.location.toString().startsWith('https://')) { - protocol = 'wss'; - } - var query = window.location.search.replace(/^\?/, '').split('&').map(function(v){ var s = v.split('='); var r = {}; @@ -18,10 +13,19 @@ var expectedLocator; if (query.locator) expectedLocator = query.locator; - var base = protocol + "://" + (window.location.href.split("://")[1]); - base = base.replace(/\/map$/, ''); - if (!base.endsWith('/')) base += '/'; - var ws_url = base + "ws/"; //guess automatically -> now default behaviour + var protocol = window.location.protocol == 'https' ? 'wss' : 'ws'; + + var href = window.location.href; + var index = href.lastIndexOf('/'); + if (index > 0) { + href = href.substr(0, index + 1); + } + href = href.split("://")[1]; + href = protocol + "://" + href; + if (!href.endsWith('/')) { + href += '/'; + } + var ws_url = href + "ws/"; var map; var markers = {}; diff --git a/htdocs/openwebrx.js b/htdocs/openwebrx.js index 38b7c09..b30a605 100644 --- a/htdocs/openwebrx.js +++ b/htdocs/openwebrx.js @@ -1523,21 +1523,23 @@ function on_ws_error() { divlog("WebSocket error.", 1); } -String.prototype.startswith = function (str) { - return this.indexOf(str) === 0; -}; //http://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string - var ws; function open_websocket() { - var protocol = 'ws'; - if (window.location.toString().startsWith('https://')) { - protocol = 'wss'; - } + var protocol = window.location.protocol == 'https' ? 'wss' : 'ws'; + + var href = window.location.href; + var index = href.lastIndexOf('/'); + if (index > 0) { + href = href.substr(0, index + 1); + } + href = href.split("://")[1]; + href = protocol + "://" + href; + if (!href.endsWith('/')) { + href += '/'; + } + var ws_url = href + "ws/"; - var base = protocol + "://" + (window.location.href.split("://")[1]); - if (!base.endsWith('/')) base += '/'; - var ws_url = base + "ws/"; //guess automatically -> now default behaviour if (!("WebSocket" in window)) divlog("Your browser does not support WebSocket, which is required for WebRX to run. Please upgrade to a HTML5 compatible browser."); ws = new WebSocket(ws_url);