Compare commits
1 Commits
main
...
1f0aa26594
| Author | SHA1 | Date | |
|---|---|---|---|
|
1f0aa26594
|
@@ -1,27 +1,32 @@
|
|||||||
const vid = document.querySelector('video');
|
const vid = document.querySelector('video');
|
||||||
const wsProto = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
const wsProto = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
||||||
let socket = null; // Initialize socket as null
|
const socket = new WebSocket(`${wsProto}://${window.location.hostname}:${window.location.port}`);
|
||||||
let retryInterval = 1000; // Initial retry interval in milliseconds
|
|
||||||
const maxRetryInterval = 30000; // Maximum retry interval
|
|
||||||
|
|
||||||
function createWebSocket() {
|
// if the new tms is within this margin of the current tms, then the change is ignored for smoother viewing
|
||||||
socket = new WebSocket(`${wsProto}://${window.location.hostname}:${window.location.port}`);
|
const PLAYING_THRESH = 1;
|
||||||
|
const PAUSED_THRESH = 0.01;
|
||||||
|
|
||||||
// Connection opened
|
// local state
|
||||||
socket.addEventListener('open', function (event) {
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
// Connection opened
|
||||||
|
socket.addEventListener('open', function (event) {
|
||||||
console.log('Connected to WS Server');
|
console.log('Connected to WS Server');
|
||||||
retryInterval = 1000; // Reset retry interval on successful connection
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// Got message from the server
|
// Got message from the server
|
||||||
socket.addEventListener("message", (event) => {
|
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
|
// Time syncing backward server-response
|
||||||
if (event.data.startsWith("time_sync_response_backward")) {
|
if (event.data.startsWith("time_sync_response_backward")) {
|
||||||
@@ -75,49 +80,16 @@ function createWebSocket() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Connection closed
|
|
||||||
socket.addEventListener('close', function (event) {
|
// Connection closed
|
||||||
|
socket.addEventListener('close', function (event) {
|
||||||
console.log('Disconnected from the WS Server');
|
console.log('Disconnected from the WS Server');
|
||||||
client_uid = null;
|
client_uid = null;
|
||||||
retryWebSocketConnection(); // Attempt to reconnect
|
setTimeout(() => window.location.reload(), 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 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
|
// Send video state update to the server
|
||||||
// event: the video event (ex: seeking, pause play)
|
// event: the video event (ex: seeking, pause play)
|
||||||
@@ -139,9 +111,7 @@ function state_change_handler(event) {
|
|||||||
client_uid: client_uid
|
client_uid: client_uid
|
||||||
};
|
};
|
||||||
|
|
||||||
if (socket.readyState === WebSocket.OPEN) {
|
|
||||||
socket.send(`state_update_from_client ${JSON.stringify(state_image)}`);
|
socket.send(`state_update_from_client ${JSON.stringify(state_image)}`);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// assigning event handlers
|
// assigning event handlers
|
||||||
@@ -243,4 +213,3 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
style.textContent = `::cue { font-size: ${size} !important; }`;
|
style.textContent = `::cue { font-size: ${size} !important; }`;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
20
src/index.js
20
src/index.js
@@ -80,14 +80,6 @@ wss.on('connection', function connection(ws) {
|
|||||||
//// Web server ////
|
//// Web server ////
|
||||||
|
|
||||||
// app settings
|
// app settings
|
||||||
|
|
||||||
// disable cache globally because it may break things between server restarts,
|
|
||||||
// since the paths aren't unique
|
|
||||||
app.use(function (req, res, next) {
|
|
||||||
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
app.use('/', express.static(__dirname));
|
app.use('/', express.static(__dirname));
|
||||||
app.use(bodyParser.urlencoded({
|
app.use(bodyParser.urlencoded({
|
||||||
extended: true
|
extended: true
|
||||||
@@ -100,6 +92,13 @@ app.use(session({
|
|||||||
logged: false
|
logged: false
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// disable cache globally because it may break things between server restarts,
|
||||||
|
// since the paths aren't unique
|
||||||
|
app.use(function (req, res, next) {
|
||||||
|
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// home page
|
// home page
|
||||||
app.get("/", function (req, res) {
|
app.get("/", function (req, res) {
|
||||||
@@ -187,10 +186,7 @@ server.listen(settings.server_port, settings.server_ip,
|
|||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
console.log("Shutting down server...");
|
console.log("Shutting down server...");
|
||||||
// wss.close();
|
// wss.close();
|
||||||
wss.clients.forEach(client => {
|
wss.clients.forEach(client => client.terminate());
|
||||||
client.send("reload_request");
|
|
||||||
client.terminate();
|
|
||||||
});
|
|
||||||
server.close(() => {
|
server.close(() => {
|
||||||
console.log("Server closed");
|
console.log("Server closed");
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ body[data-background] {
|
|||||||
background-size: cover;
|
background-size: cover;
|
||||||
background-position: center;
|
background-position: center;
|
||||||
background-repeat: no-repeat;
|
background-repeat: no-repeat;
|
||||||
background-blend-mode: multiply;
|
background-blend-mode: darken;
|
||||||
}
|
}
|
||||||
|
|
||||||
.container {
|
.container {
|
||||||
@@ -39,7 +39,6 @@ header {
|
|||||||
video {
|
video {
|
||||||
/*height:;*/
|
/*height:;*/
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
max-height: 80vh;
|
|
||||||
/*max-height: 400px;*/
|
/*max-height: 400px;*/
|
||||||
/*position: absolute;*/
|
/*position: absolute;*/
|
||||||
/*top: 0;*/
|
/*top: 0;*/
|
||||||
|
|||||||
Reference in New Issue
Block a user