Skip to content

Commit

Permalink
🐛 Fix bug where socket would try to reconnect far too many times.
Browse files Browse the repository at this point in the history
  • Loading branch information
hayleigh-dot-dev committed Jun 14, 2024
1 parent 650c932 commit ffb31a5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions priv/server/live-reload.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
let socket = null;
let timeout = null;

function connect() {
socket = new WebSocket(`ws://${window.location.host}/lustre-dev-tools`);

if (timeout) {
clearTimeout(timeout);
timeout = null;
}

socket.onmessage = (event) => {
if (event.data === "reload") {
window.location.reload();
Expand All @@ -15,12 +21,12 @@ function connect() {
// refresh.
socket.onclose = () => {
socket = null;
setTimeout(() => connect(), 5000);
if (!timeout) timeout = setTimeout(() => connect(), 5000);
};

socket.onerror = () => {
socket = null;
setTimeout(() => connect(), 5000);
if (!timeout) timeout = setTimeout(() => connect(), 5000);
};
}

Expand Down

0 comments on commit ffb31a5

Please sign in to comment.