Skip to content

Commit

Permalink
new websocket init (a lot simpler (like in v0.103))
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Oct 13, 2024
1 parent 031b1f7 commit 8165b1a
Showing 1 changed file with 6 additions and 45 deletions.
51 changes: 6 additions & 45 deletions htag/runners/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 8165b1a

Please sign in to comment.