Skip to content

Commit

Permalink
Avoid websocket reconnection from hidden tabs (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi authored Jan 14, 2025
1 parent 81cb02b commit 612313a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/viser/client/src/WebsocketServerWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ function collectArrayBuffers(obj: any, buffers: Set<ArrayBuffer>) {
console.log(`Disconnected! ${server} code=${event.code}`);
clearTimeout(retryTimeout);

// Try to reconnect.
if (server !== null) setTimeout(tryConnect, 1000);
// Try to reconnect on next repaint.
// requestAnimationFrame() helps us avoid reconnecting from tabs that are
// hidden or minimized.
if (server !== null) {
requestAnimationFrame(() => {
setTimeout(tryConnect, 1000);
});
}
};

ws.onmessage = async (event) => {
Expand Down

0 comments on commit 612313a

Please sign in to comment.