Add some better balancing, reconnection
This commit is contained in:
parent
715f79c9c0
commit
324ba0d160
6 changed files with 77 additions and 29 deletions
|
|
@ -17,25 +17,40 @@ languagePluginLoader.then(() => {
|
|||
wsprotocol = "wss:"
|
||||
}
|
||||
var wspath = wsprotocol + "//" + window.location.host + "/api/ws"
|
||||
var c = new WebSocket(wspath)
|
||||
c.onmessage = function(msg) {
|
||||
var workText = workTerms[Math.floor(Math.random() * workTerms.length)];
|
||||
$("#output").append("<p>" + workText + "...</p>")
|
||||
pyodide.globals.torun = msg.data
|
||||
pyodide.runPythonAsync("exec_work(torun)").then((res) => {
|
||||
$("#output").append("<p>Did work! 👏</p>")
|
||||
c.send(res)
|
||||
})
|
||||
}
|
||||
c.onopen = function() {
|
||||
$("#status").text("Status: connected!")
|
||||
c.send(JSON.stringify({
|
||||
status: 2,
|
||||
error_msg: "WebsocketWorker"
|
||||
}))
|
||||
}
|
||||
c.onclose = function() {
|
||||
$("#status").text("Status: disconnected")
|
||||
}
|
||||
})
|
||||
})
|
||||
function connect() {
|
||||
var c = new WebSocket(wspath)
|
||||
|
||||
c.onopen = function() {
|
||||
$("#status").text("Status: connected!")
|
||||
c.send(JSON.stringify({
|
||||
status: 2,
|
||||
error_msg: "WebsocketWorker"
|
||||
}))
|
||||
}
|
||||
|
||||
c.onmessage = function(msg) {
|
||||
var workText = workTerms[Math.floor(Math.random() * workTerms.length)];
|
||||
$("#output").append("<p>" + workText + "...</p>")
|
||||
pyodide.globals.torun = msg.data
|
||||
pyodide.runPythonAsync("exec_work(torun)").then((res) => {
|
||||
$("#output").append("<p>Did work! 👏</p>")
|
||||
c.send(res)
|
||||
})
|
||||
};
|
||||
|
||||
c.onclose = function(e) {
|
||||
$("#status").text("Status: disconnected. reconnecting...")
|
||||
console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason);
|
||||
setTimeout(function() {
|
||||
connect();
|
||||
}, 500);
|
||||
};
|
||||
|
||||
c.onerror = function(err) {
|
||||
console.error('Socket encountered error: ', err.message, 'Closing socket');
|
||||
c.close();
|
||||
};
|
||||
};
|
||||
|
||||
connect();
|
||||
}) })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue