websocket reconnects automatically

This commit is contained in:
iou1name 2020-08-14 23:29:10 -04:00
parent 1c179843f6
commit 84ac3813ed

View File

@ -1,7 +1,7 @@
var socket;
function load() {
socket = init_websocket();
init_websocket();
document.querySelector('#player').addEventListener('ended', function() {
if (document.querySelector('#shuffle').checked) {
socket.send_event('random_track', {});
@ -11,16 +11,18 @@ function load() {
/* Websocket setup */
function init_websocket() {
let socket = new WebSocket('wss://' + window.location.hostname + ws_uri);
socket.send_event = send_event;
socket.onmessage = onmessage;
socket.onclose = onclose;
socket.onerror = onerror;
socket.events = {};
socket.events['artist_albums'] = artist_albums_recv;
socket.events['album_tracks'] = album_tracks_recv;
socket.events['track'] = track_recv;
return socket;
let _socket;
_socket = new WebSocket('wss://' + window.location.hostname + ws_uri);
_socket.send_event = send_event;
_socket.onopen = onopen;
_socket.onmessage = onmessage;
_socket.onclose = onclose;
_socket.onerror = onerror;
_socket.events = {};
_socket.events['artist_albums'] = artist_albums_recv;
_socket.events['album_tracks'] = album_tracks_recv;
_socket.events['track'] = track_recv;
window.socket = _socket;
}
function send_event(event_title, data) {
@ -79,7 +81,7 @@ async function onclose(event) {
console.log(event);
console.log('Websocket lost connection to server. Re-trying...');
await sleep(3000);
socket = init_websocket();
init_websocket();
}
function sleep(ms) {