a | b/test.html | ||
---|---|---|---|
1 | <!DOCTYPE html> |
||
2 | <html lang="en"> |
||
3 | <head> |
||
4 | <meta charset="UTF-8"> |
||
5 | <title>Title</title> |
||
6 | </head> |
||
7 | <body onload="onBodyLoad()"> |
||
8 | <script> |
||
9 | window.addEventListener("message", receiveMessage, false); |
||
10 | |||
11 | let url0, url1, activeUrl, urlSet = false, timer = -1; |
||
12 | |||
13 | function onBodyLoad() { |
||
14 | url0 = document.getElementById("url0"); |
||
15 | url1 = document.getElementById("url1"); |
||
16 | } |
||
17 | |||
18 | function receiveMessage(event) { |
||
19 | if (activeUrl.startsWith(event.origin)) { |
||
20 | if (timer !== -1) { |
||
21 | clearTimeout(timer); |
||
22 | timer = -1; |
||
23 | console.log("Received messages. Killing timer!"); |
||
24 | } |
||
25 | var message = event.data; |
||
26 | console.log(message); |
||
27 | if (message === "--INFO--\"Still polling the registration of the visualization. Is the visu stopped?\"") { |
||
28 | console.log("Webvisu stopped? Switching to different url"); |
||
29 | setTimeout(switchToDifferentWebvisu, 1000); |
||
30 | } |
||
31 | if (message === "Error while processing the visualization: Sending service aborted") { |
||
32 | console.log("Download? Switching to different url"); |
||
33 | setTimeout(switchToDifferentWebvisu, 1000); |
||
34 | } |
||
35 | if (message === "--INFO--\"Triing to reconnect after error\"") { |
||
36 | console.log("Error? Switching to different url"); |
||
37 | setTimeout(switchToDifferentWebvisu, 1000); |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | |||
42 | function setURL() { |
||
43 | document.getElementById('frame').src = url0.value; |
||
44 | url0.disabled = true; |
||
45 | url1.disabled = true; |
||
46 | document.getElementById("button").disabled = true; |
||
47 | activeUrl = url0.value; |
||
48 | urlSet = true; |
||
49 | } |
||
50 | |||
51 | function onLoad() { |
||
52 | if (urlSet) { |
||
53 | timer = setTimeout(function () { |
||
54 | console.log("No message received after 5 seconds, switching to different webvisu."); |
||
55 | switchToDifferentWebvisu(); |
||
56 | }, 5000); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | function switchToDifferentWebvisu() { |
||
61 | if (activeUrl === url0.value) { |
||
62 | activeUrl = url1.value; |
||
63 | } else if (activeUrl === url1.value) { |
||
64 | activeUrl = url0.value; |
||
65 | } else { |
||
66 | console.error("Unexpected settings! Abort!"); |
||
67 | return; |
||
68 | } |
||
69 | document.getElementById('frame').src = activeUrl; |
||
70 | } |
||
71 | |||
72 | function onError() { |
||
73 | console.log("onError"); |
||
74 | } |
||
75 | </script> |
||
76 | <iframe id="frame" onload="onLoad()" onerror="onError()"></iframe> |
||
77 | <br><br> |
||
78 | <label for="url0">Url0:</label> |
||
79 | <input type="text" id="url0" name="url0" value="http://localhost:8080/webvisu.htm"><br><br> |
||
80 | <label for="url1">Url1:</label> |
||
81 | <input type="text" id="url1" name="url1" value="http://localhost:8080/webvisu.htm"><br><br> |
||
82 | <button type="button" id="button" onclick="setURL()">Change</button> |
||
83 | |||
84 | |||
85 | </body> |
||
86 | </html> |