improve websocket url determination

This commit is contained in:
Jakob Ketterl 2019-12-03 18:53:57 +01:00
parent 15b860af36
commit fc8d3d8f11
2 changed files with 26 additions and 20 deletions

View File

@ -1,9 +1,4 @@
(function(){ (function(){
var protocol = 'ws';
if (window.location.toString().startsWith('https://')) {
protocol = 'wss';
}
var query = window.location.search.replace(/^\?/, '').split('&').map(function(v){ var query = window.location.search.replace(/^\?/, '').split('&').map(function(v){
var s = v.split('='); var s = v.split('=');
var r = {}; var r = {};
@ -18,10 +13,19 @@
var expectedLocator; var expectedLocator;
if (query.locator) expectedLocator = query.locator; if (query.locator) expectedLocator = query.locator;
var base = protocol + "://" + (window.location.href.split("://")[1]); var protocol = window.location.protocol == 'https' ? 'wss' : 'ws';
base = base.replace(/\/map$/, '');
if (!base.endsWith('/')) base += '/'; var href = window.location.href;
var ws_url = base + "ws/"; //guess automatically -> now default behaviour 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 map;
var markers = {}; var markers = {};

View File

@ -1523,21 +1523,23 @@ function on_ws_error() {
divlog("WebSocket error.", 1); 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; var ws;
function open_websocket() { function open_websocket() {
var protocol = 'ws'; var protocol = window.location.protocol == 'https' ? 'wss' : 'ws';
if (window.location.toString().startsWith('https://')) {
protocol = 'wss'; 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)) 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."); 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); ws = new WebSocket(ws_url);