sS hInitSecondaryWebSocket(O... _) { optPar S wsVar = "ws2"; optPar S url; if (empty(url)) fail("Need URL for secondary websocket"); optPar S onOpen; optPar S onMessage; optPar S readyMsg = "WebSocket 2 ready!"; ret hreconnectingWebSockets() + hscript([[ var ws2Ready = false; var ws2InitialMsgs = []; var ws2Verbose = false; var ]] + wsVar + [[ = new ReconnectingWebSocket(]] + jsQuote(url) + [[); ]] + wsVar + [[.onopen = function(event) { ws2Ready = true; console.log(]] + jsQuote(readyMsg) + [[); ws2InitialMsgs.forEach(function(msg) { if (ws2Verbose) console.log("Sending initial msg to websocket 2: " + msg); ws2.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 ws2InitialMsg(msg) { ws2InitialMsgs.push(msg); if (ws2Ready) { if (ws2Verbose) console.log("Sending initial msg to websocket 2: " + msg); ws2.send(msg); } } ]]); }