// defines: // variable ws (the websocket) // variable wsReady (boolean) // wsSend(msg) (send a message when websocket is ready) sclass HInitWebSocket { S wsVar = "ws"; S onOpen; S onMessage; S readyMsg = "WebSocket ready!"; S headStuff aka get() { ret hreconnectingWebSockets() + hscript([[ var wsReady = false; var wsInitialMsgs = []; var wsVerbose = false; var ]] + wsVar + [[ = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/"); ]] + wsVar + [[.onopen = function(event) { wsReady = true; console.log(]] + jsQuote(readyMsg) + [[); wsInitialMsgs.forEach(function(msg) { if (wsVerbose) console.log("Sending initial msg: " + msg); ws.send(msg); }); wsInitialMsgs = []; ]] + jsDropTrailingComments(onOpen) + [[ }; ]] + wsVar + [[.onmessage = function(event) { ]] + unnull(onMessage) + [[ }; // send it now if ws is open already or when ws was opened function wsSend(msg) { if (wsReady) { wsInitialMsgs.push(msg); if (wsVerbose) console.log("Sending initial msg: " + msg); ws.send(msg); } } ]]); } }