From 8165b1a5855c906754a35ad99cf5d395c26b2e6f Mon Sep 17 00:00:00 2001 From: manatlan Date: Sun, 13 Oct 2024 16:56:54 +0200 Subject: [PATCH] new websocket init (a lot simpler (like in v0.103)) --- htag/runners/runner.py | 51 +++++------------------------------------- 1 file changed, 6 insertions(+), 45 deletions(-) diff --git a/htag/runners/runner.py b/htag/runners/runner.py index 69683d8..d748501 100644 --- a/htag/runners/runner.py +++ b/htag/runners/runner.py @@ -212,53 +212,14 @@ async def hrinteract(self, hr:HRenderer, jzon:str) -> str: class ServerWS(MyServer): jsinteract = """ -function connectWebSocket(url, timeout) { - timeout = timeout || 2000; - return new Promise(function(resolve, reject) { - const socket = new WebSocket(url); - - const timer = setTimeout(function() { - reject(new Error("webSocket timeout")); - done(); - socket.close(); - }, timeout); - - function done() { - clearTimeout(timer); - socket.removeEventListener('error', error); - } - - function error(e) { - reject(e); - done(); - } - - socket.addEventListener('open', function() { - resolve(socket); - done(); - }); - socket.addEventListener('error', error); - }); -} - async function interact( o ) { - WS.send( JSON.stringify(o) ); -} - -var WS=null; -let _url = document.location.host+"%s"; - -function _ws_conected(socket) { - console.info("WS connected:",socket.url); - WS=socket; - WS.onmessage = function(e) { - action( e.data ); - }; - start(); + ws.send( JSON.stringify(o) ); } -connectWebSocket("ws://"+_url,500).then(_ws_conected).catch(function(err) { - connectWebSocket("wss://"+_url,2000).then(_ws_conected); -}); +var ws = new WebSocket("//"+document.location.host+"%s"); +ws.onopen = start; +ws.onmessage = function(e) { + action( e.data ); +}; """ def run(self):