Changed spacing in html and JS files.

Changed spacing based to tab based
This commit is contained in:
ShakedAp
2023-06-26 20:23:35 +03:00
parent 8227bb1861
commit 3fd791dce7
4 changed files with 206 additions and 204 deletions

View File

@@ -20,93 +20,88 @@ let correction = 0
// Connection opened
socket.addEventListener('open', function (event) {
console.log('Connected to WS Server');
console.log('Connected to WS Server');
});
// Got message from the server
socket.addEventListener("message", (event) => {
if (event.data.startsWith("time_sync_response_backward"))
{
let time_at_server = Number(event.data.slice("time_sync_response_backward".length + 1));
let under_estimate_latest = time_at_server - get_global_time(0);
if (event.data.startsWith("time_sync_response_backward")) {
let time_at_server = Number(event.data.slice("time_sync_response_backward".length + 1));
let under_estimate_latest = time_at_server - get_global_time(0);
under_estimates.push(under_estimate_latest);
under_estimate = median(under_estimates);
correction = (under_estimate + over_estimate)/2;
under_estimates.push(under_estimate_latest);
under_estimate = median(under_estimates);
correction = (under_estimate + over_estimate) / 2;
console.log(`%c Updated val for under_estimate is ${under_estimate}`, "color:green");
console.log(`%c New correction time is ${correction} miliseconds`, 'color:red; font-size:12px');
}
if (event.data.startsWith("time_sync_response_forward"))
{
let calculated_diff = Number(event.data.slice("time_sync_response_forward".length + 1));
over_estimates.push(calculated_diff);
over_estimate = median(over_estimates);
correction = (under_estimate + over_estimate)/2;
console.log(`%c Updated val for under_estimate is ${under_estimate}`, "color:green");
console.log(`%c New correction time is ${correction} miliseconds`, 'color:red; font-size:12px');
}
if (event.data.startsWith("time_sync_response_forward")) {
let calculated_diff = Number(event.data.slice("time_sync_response_forward".length + 1));
over_estimates.push(calculated_diff);
over_estimate = median(over_estimates);
correction = (under_estimate + over_estimate) / 2;
console.log(`%c Updated val for over_estimate is ${over_estimate}`, "color:green");
console.log(`%c New correction time is ${correction} miliseconds`, 'color:red; font-size:12px');
}
if (event.data.startsWith("state_update_from_server"))
{
let state = JSON.parse(event.data.slice("state_update_from_server".length + 1));
console.log(`%c Updated val for over_estimate is ${over_estimate}`, "color:green");
console.log(`%c New correction time is ${correction} miliseconds`, 'color:red; font-size:12px');
}
if (event.data.startsWith("state_update_from_server")) {
let state = JSON.parse(event.data.slice("state_update_from_server".length + 1));
// Whenever the client connects or reconnects
if (client_uid == null){
client_uid = state.client_uid;
}
// Whenever the client connects or reconnects
if (client_uid == null) {
client_uid = state.client_uid;
}
// calculating the new timestamp for both cases - when the video is playing and when it is paused
let proposed_time = (state.playing) ? ((get_global_time(correction) - state.global_timestamp)/1000 + state.video_timestamp) : (state.video_timestamp)
let gap = Math.abs(proposed_time - vid.currentTime)
// calculating the new timestamp for both cases - when the video is playing and when it is paused
let proposed_time = (state.playing) ? ((get_global_time(correction) - state.global_timestamp) / 1000 + state.video_timestamp) : (state.video_timestamp)
let gap = Math.abs(proposed_time - vid.currentTime)
console.log(`%cGap was ${proposed_time - vid.currentTime}`, 'font-size:12px; color:purple')
if (state.playing){
// tolerance while the video is playing
if(gap > PLAYING_THRESH){
vid.currentTime = proposed_time
}
vid.play()
}
else{
vid.pause()
// condition to prevent an unnecessary seek
if (gap > PAUSED_THRESH){
vid.currentTime = proposed_time
}
}
}
console.log(`%cGap was ${proposed_time - vid.currentTime}`, 'font-size:12px; color:purple')
if (state.playing) {
// tolerance while the video is playing
if (gap > PLAYING_THRESH) {
vid.currentTime = proposed_time
}
vid.play()
} else {
vid.pause()
// condition to prevent an unnecessary seek
if (gap > PAUSED_THRESH) {
vid.currentTime = proposed_time
}
}
}
});
// Connection closed
socket.addEventListener('close', function (event) {
console.log('Disconnected from the WS Server');
client_uid = null
console.log('Disconnected from the WS Server');
client_uid = null
});
function state_change_handler(event)
{
if (event !== null && event !== undefined){
function state_change_handler(event) {
if (event !== null && event !== undefined) {
if (event.type === 'pause')
video_playing = false;
else if (event.type === 'play')
video_playing = true;
}
last_updated = get_global_time(correction);
last_updated = get_global_time(correction);
state_image = {
video_timestamp: vid.currentTime,
last_updated: last_updated,
playing: video_playing,
global_timestamp: get_global_time(correction),
client_uid: client_uid
client_uid: client_uid
}
socket.send(`state_update_from_client ${JSON.stringify(state_image)}`)
socket.send(`state_update_from_client ${JSON.stringify(state_image)}`)
}
// assigning event handlers
@@ -130,17 +125,21 @@ function get_global_time(delta = 0) {
}
async function get_settings() {
let s = null;
await fetch('settings.json')
.then((response)=>response.json())
.then((responseJson)=>{s = responseJson});
return s;
let s = null;
await fetch('settings.json')
.then((response) => response.json())
.then((responseJson) => {
s = responseJson
});
return s;
}
function median(values) {
if(values.length === 0) { return 0; }
if (values.length === 0) {
return 0;
}
values.sort((x,y) => (x-y));
values.sort((x, y) => (x - y));
let half = Math.floor(values.length / 2);
if (values.length % 2) {
return values[half];
@@ -149,21 +148,22 @@ function median(values) {
}
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
return new Promise(resolve => setTimeout(resolve, ms));
}
function do_time_sync_one_cycle_backward() {
socket.send("time_sync_request_backward");
socket.send("time_sync_request_backward");
}
function do_time_sync_one_cycle_forward() {
socket.send(`time_sync_request_forward ${get_global_time()}`);
socket.send(`time_sync_request_forward ${get_global_time()}`);
}
// time requests are made every second
async function do_time_sync() {
for(let i = 0; i < num_time_sync_cycles; i++){
for (let i = 0; i < num_time_sync_cycles; i++) {
await timeout(500);
do_time_sync_one_cycle_backward();
await timeout(500);