sS hInitWebSocket(O... _) { optPar S wsVar = "ws"; optPar S onOpen; optPar S onMessage; optPar S readyMsg = "WebSocket ready!"; 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); }); ]] + jsDropTrailingComments(onOpen) + [[ }; ]] + wsVar + [[.onmessage = function(event) { ]] + unnull(onMessage) + [[ }; // add an initial message to send when websocket opens // also send it now if ws is open already function wsInitialMsg(msg) { wsInitialMsgs.push(msg); if (wsReady) { if (wsVerbose) console.log("Sending initial msg: " + msg); ws.send(msg); } } ]]); }