c3lf-system-3/web/public/js/worker.js
2024-04-25 23:54:06 +02:00

50 lines
1.4 KiB
JavaScript

let state = 0;
let ports = [];
let notify_socket;
function tryConnect(){
if(self.WebSocket === undefined){
console.log("no websocket support");
return;
}
if (!notify_socket || notify_socket.readyState !== WebSocket.OPEN) {
const scheme = location.protocol === "https:" ? "wss" : "ws";
notify_socket = new WebSocket(scheme + '://' + location.host + '/ws/2/notify/');
notify_socket.onopen = (e) => {
console.log("open", JSON.stringify(e));
};
notify_socket.onclose = (e) => {
console.log("close", JSON.stringify(e));
setTimeout(() => {
tryConnect();
}, 1000);
};
notify_socket.onerror = (e) => {
console.log("error", JSON.stringify(e));
setTimeout(() => {
tryConnect();
}, 1000);
};
notify_socket.onmessage = (e) => {
let data = JSON.parse(e.data);
console.log("message", data);
//this.loadEventItems()
//this.loadTickets()
}
}
}
onconnect = function (e) {
const port = e.ports[0];
ports.push(port);
port.onmessage = function (e) {
state = state + 1;
for (let i = 0; i < ports.length; i++) {
ports[i].postMessage({d: e.data, s: state, n: ports.length});
}
}
port.start();
console.log("worker connected", JSON.stringify(e));
tryConnect();
}