Try WSS first, and fallback to WS (#144)

This commit is contained in:
Alex Ling 2021-01-12 10:12:02 +00:00
parent 0b457a2797
commit a4c6e6611c

View File

@ -4,21 +4,30 @@ const component = () => {
paused: undefined, paused: undefined,
loading: false, loading: false,
toggling: false, toggling: false,
ws: undefined,
init() { wsConnect(secure = true) {
const ws = new WebSocket(`ws://${location.host}${base_url}api/admin/mangadex/queue`); const url = `${secure ? 'wss' : 'ws'}://${location.host}${base_url}api/admin/mangadex/queue`;
ws.onmessage = event => { console.log(`Connecting to ${url}`);
this.ws = new WebSocket(url);
this.ws.onmessage = event => {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
this.jobs = data.jobs; this.jobs = data.jobs;
this.paused = data.paused; this.paused = data.paused;
}; };
ws.onerror = err => { this.ws.onclose = () => {
alert('danger', `Socket connection failed. Error: ${err}`); if (this.ws.failed)
return this.wsConnect(false);
alert('danger', 'Socket connection closed');
}; };
ws.onclose = err => { this.ws.onerror = () => {
if (secure)
return this.ws.failed = true;
alert('danger', 'Socket connection failed'); alert('danger', 'Socket connection failed');
}; };
},
init() {
this.wsConnect();
this.load(); this.load();
}, },
load() { load() {