graceful handling of disconnects and page reloads

This commit is contained in:
Yuri Tatishchev 2025-06-19 00:52:35 -07:00
parent 302113baee
commit 06d5336d4b
Signed by: CaZzzer
SSH Key Fingerprint: SHA256:sqXB3fe0LMpfH+IeM/vlmxKdso52kssrIJBlwKXVe1U
2 changed files with 109 additions and 75 deletions

View File

@ -1,33 +1,28 @@
const vid = document.querySelector('video');
const wsProto = window.location.protocol === 'https:' ? 'wss' : 'ws';
const socket = new WebSocket(`${wsProto}://${window.location.hostname}:${window.location.port}`);
// if the new tms is within this margin of the current tms, then the change is ignored for smoother viewing
const PLAYING_THRESH = 1;
const PAUSED_THRESH = 0.01;
// local state
let video_playing = false;
let last_updated = 0;
let client_uid = null;
// clocks sync variables
const num_time_sync_cycles = 10;
let over_estimates = new Array();
let under_estimates = new Array();
let over_estimate = 0;
let under_estimate = 0;
let correction = 0;
let socket = null; // Initialize socket as null
let retryInterval = 1000; // Initial retry interval in milliseconds
const maxRetryInterval = 30000; // Maximum retry interval
function createWebSocket() {
socket = new WebSocket(`${wsProto}://${window.location.hostname}:${window.location.port}`);
// Connection opened
socket.addEventListener('open', function (event) {
console.log('Connected to WS Server');
retryInterval = 1000; // Reset retry interval on successful connection
});
// Got message from the server
socket.addEventListener("message", (event) => {
// Reload the page if the server sends a reload request
if (event.data.startsWith("reload_request")) {
console.log("Reloading the page as requested by the server");
setTimeout(() => window.location.reload(), 1000);
return;
}
// Time syncing backward server-response
if (event.data.startsWith("time_sync_response_backward")) {
let time_at_server = Number(event.data.slice("time_sync_response_backward".length + 1));
@ -82,14 +77,47 @@ socket.addEventListener("message", (event) => {
}
});
// Connection closed
socket.addEventListener('close', function (event) {
console.log('Disconnected from the WS Server');
client_uid = null;
setTimeout(() => window.location.reload(), 1000);
retryWebSocketConnection(); // Attempt to reconnect
});
// Connection error
socket.addEventListener('error', function (event) {
console.error('WebSocket error:', event);
socket.close(); // Ensure the socket is closed on error
});
}
function retryWebSocketConnection() {
console.log(`Attempting to reconnect in ${retryInterval / 1000} seconds...`);
setTimeout(() => {
retryInterval = Math.min(retryInterval * 2, maxRetryInterval); // Exponential backoff
createWebSocket(); // Recreate the WebSocket connection
}, retryInterval);
}
// Initial WebSocket connection
createWebSocket();
// if the new tms is within this margin of the current tms, then the change is ignored for smoother viewing
const PLAYING_THRESH = 1;
const PAUSED_THRESH = 0.01;
// local state
let video_playing = false;
let last_updated = 0;
let client_uid = null;
// clocks sync variables
const num_time_sync_cycles = 10;
let over_estimates = new Array();
let under_estimates = new Array();
let over_estimate = 0;
let under_estimate = 0;
let correction = 0;
// Send video state update to the server
// event: the video event (ex: seeking, pause play)
@ -111,8 +139,10 @@ function state_change_handler(event) {
client_uid: client_uid
};
if (socket.readyState === WebSocket.OPEN) {
socket.send(`state_update_from_client ${JSON.stringify(state_image)}`);
}
}
// assigning event handlers
vid.onseeking = state_change_handler;
@ -213,3 +243,4 @@ document.addEventListener('DOMContentLoaded', function() {
style.textContent = `::cue { font-size: ${size} !important; }`;
});
});

View File

@ -187,7 +187,10 @@ server.listen(settings.server_port, settings.server_ip,
process.on('SIGINT', () => {
console.log("Shutting down server...");
// wss.close();
wss.clients.forEach(client => client.terminate());
wss.clients.forEach(client => {
client.send("reload_request");
client.terminate();
});
server.close(() => {
console.log("Server closed");
process.exit(0);