|
| 1 | +""" |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2017 - Present PythonistaGuild |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +OVERLAY_HTML = """<html> |
| 26 | + <body> |
| 27 | + <main id="container"></main> |
| 28 | + <script> |
| 29 | + const WSAddr = {WEBSOCKET_ADDR}; |
| 30 | + const timeout = {TIMEOUT}; |
| 31 | +
|
| 32 | + let isConnected = false; |
| 33 | + let sock = new WebSocket(WSAddr); |
| 34 | + let reconnectInterval = null; |
| 35 | + let pingInterval = null; |
| 36 | +
|
| 37 | + const setup = () => {{ |
| 38 | + if (reconnectInterval !== null) {{ |
| 39 | + clearReconnect(); |
| 40 | + }}; |
| 41 | +
|
| 42 | + sock.addEventListener("open", (e) => {{ |
| 43 | + console.log("Websocket connection successfully opened."); |
| 44 | +
|
| 45 | + clearReconnect(); |
| 46 | +
|
| 47 | + if (pingInterval === null) {{ |
| 48 | + pingInterval = setInterval(pingTask, 10000); |
| 49 | + }}; |
| 50 | + }}); |
| 51 | +
|
| 52 | + sock.addEventListener("close", (e) => {{ |
| 53 | + console.log("Websocket connection closed."); |
| 54 | + isConnected = false; |
| 55 | +
|
| 56 | + if (pingInterval !== null) {{ |
| 57 | + clearInterval(pingInterval); |
| 58 | + pingInterval = null; |
| 59 | + }}; |
| 60 | +
|
| 61 | + // Try to re-establish connection... |
| 62 | + reconnectInterval = setInterval(reconnect, timeout); |
| 63 | + }}); |
| 64 | +
|
| 65 | + sock.addEventListener("message", (e) => {{ |
| 66 | + const data = JSON.parse(e.data); |
| 67 | + let nodes = data["nodes"]; |
| 68 | + let delay = data["duration"]; |
| 69 | +
|
| 70 | + let container = document.getElementById("container"); |
| 71 | +
|
| 72 | + for (let node of nodes) {{ |
| 73 | + console.log(node["raw"]); |
| 74 | + container.insertAdjacentHTML("beforeend", node["raw"]); |
| 75 | +
|
| 76 | + if (node["type"] === "audio") {{ |
| 77 | + let audio = document.getElementById(node["html_id"]); |
| 78 | + audio.play(); |
| 79 | + }}; |
| 80 | + }}; |
| 81 | + }}); |
| 82 | + }}; |
| 83 | +
|
| 84 | + const clearOverlay = () => {{ |
| 85 | + let container = document.getElementById("container"); |
| 86 | + container.innerHTML = ""; |
| 87 | + }}; |
| 88 | +
|
| 89 | + const clearReconnect = () => {{ |
| 90 | + clearInterval(reconnectInterval); |
| 91 | + reconnectInterval = null; |
| 92 | + isConnected = true; |
| 93 | + }}; |
| 94 | +
|
| 95 | + const pingTask = () => {{ |
| 96 | + sock.send("ping"); |
| 97 | + }}; |
| 98 | +
|
| 99 | + const reconnect = () => {{ |
| 100 | + console.log("Attempting to reconnect to websocket."); |
| 101 | +
|
| 102 | + if (isConnected) {{ |
| 103 | + clearReconnect(); |
| 104 | + return |
| 105 | + }}; |
| 106 | +
|
| 107 | + try {{ |
| 108 | + sock = new WebSocket(WSAddr); |
| 109 | + }} catch (error) {{ |
| 110 | + console.error("Unable to connect to websocket."); |
| 111 | + return |
| 112 | + }}; |
| 113 | +
|
| 114 | + setup(); |
| 115 | + }}; |
| 116 | +
|
| 117 | + setup(); |
| 118 | + </script> |
| 119 | + </body> |
| 120 | +</html>""" |
0 commit comments