--- a +++ b/test.html @@ -0,0 +1,86 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Title</title> +</head> +<body onload="onBodyLoad()"> +<script> + window.addEventListener("message", receiveMessage, false); + + let url0, url1, activeUrl, urlSet = false, timer = -1; + + function onBodyLoad() { + url0 = document.getElementById("url0"); + url1 = document.getElementById("url1"); + } + + function receiveMessage(event) { + if (activeUrl.startsWith(event.origin)) { + if (timer !== -1) { + clearTimeout(timer); + timer = -1; + console.log("Received messages. Killing timer!"); + } + var message = event.data; + console.log(message); + if (message === "--INFO--\"Still polling the registration of the visualization. Is the visu stopped?\"") { + console.log("Webvisu stopped? Switching to different url"); + setTimeout(switchToDifferentWebvisu, 1000); + } + if (message === "Error while processing the visualization: Sending service aborted") { + console.log("Download? Switching to different url"); + setTimeout(switchToDifferentWebvisu, 1000); + } + if (message === "--INFO--\"Triing to reconnect after error\"") { + console.log("Error? Switching to different url"); + setTimeout(switchToDifferentWebvisu, 1000); + } + } + } + + function setURL() { + document.getElementById('frame').src = url0.value; + url0.disabled = true; + url1.disabled = true; + document.getElementById("button").disabled = true; + activeUrl = url0.value; + urlSet = true; + } + + function onLoad() { + if (urlSet) { + timer = setTimeout(function () { + console.log("No message received after 5 seconds, switching to different webvisu."); + switchToDifferentWebvisu(); + }, 5000); + } + } + + function switchToDifferentWebvisu() { + if (activeUrl === url0.value) { + activeUrl = url1.value; + } else if (activeUrl === url1.value) { + activeUrl = url0.value; + } else { + console.error("Unexpected settings! Abort!"); + return; + } + document.getElementById('frame').src = activeUrl; + } + + function onError() { + console.log("onError"); + } +</script> +<iframe id="frame" onload="onLoad()" onerror="onError()"></iframe> +<br><br> +<label for="url0">Url0:</label> +<input type="text" id="url0" name="url0" value="http://localhost:8080/webvisu.htm"><br><br> +<label for="url1">Url1:</label> +<input type="text" id="url1" name="url1" value="http://localhost:8080/webvisu.htm"><br><br> +<button type="button" id="button" onclick="setURL()">Change</button> + + +</body> +</html>