From 84ac3813ed79a07e44125ddeffca287d5e34f1a9 Mon Sep 17 00:00:00 2001 From: iou1name Date: Fri, 14 Aug 2020 23:29:10 -0400 Subject: [PATCH] websocket reconnects automatically --- static/scorch.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/static/scorch.js b/static/scorch.js index 15df1b8..9e4f2b0 100644 --- a/static/scorch.js +++ b/static/scorch.js @@ -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) {