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

@ -26,53 +26,49 @@ socket.addEventListener('open', function (event) {
// Got message from the server
socket.addEventListener("message", (event) => {
if (event.data.startsWith("time_sync_response_backward"))
{
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;
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"))
{
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;
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"))
{
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){
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 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){
if (state.playing) {
// tolerance while the video is playing
if(gap > PLAYING_THRESH){
if (gap > PLAYING_THRESH) {
vid.currentTime = proposed_time
}
vid.play()
}
else{
} else {
vid.pause()
// condition to prevent an unnecessary seek
if (gap > PAUSED_THRESH){
if (gap > PAUSED_THRESH) {
vid.currentTime = proposed_time
}
}
@ -87,9 +83,8 @@ socket.addEventListener('close', function (event) {
});
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;
@ -132,15 +127,19 @@ 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});
.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];
@ -156,6 +155,7 @@ function timeout(ms) {
function do_time_sync_one_cycle_backward() {
socket.send("time_sync_request_backward");
}
function do_time_sync_one_cycle_forward() {
socket.send(`time_sync_request_forward ${get_global_time()}`);
}
@ -163,7 +163,7 @@ function do_time_sync_one_cycle_forward() {
// 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);

View File

@ -6,7 +6,9 @@ const WebSocket = require('ws');
const app = express();
const server = require('http').createServer(app);
const wss = new WebSocket.Server({ server:server });
const wss = new WebSocket.Server({
server: server
});
const settings = JSON.parse(fs.readFileSync("settings.json"));
const THRESH_IGNORANCE = 250;
@ -24,7 +26,7 @@ wss.on('connection', function connection(ws) {
users_amount += 1;
console.log('A new client Connected. Amount of users: ', users_amount);
state.client_uid = unique_id;
unique_id +=1 ;
unique_id += 1;
ws.send(`state_update_from_server ${JSON.stringify(state)}`);
ws.on('error', console.error);
@ -32,24 +34,20 @@ wss.on('connection', function connection(ws) {
ws.on('message', function message(data) {
data = data.toString();
if(data.startsWith("time_sync_request_backward"))
{
if (data.startsWith("time_sync_request_backward")) {
ws.send(`time_sync_response_backward ${get_time()}`);
}
if(data.startsWith("time_sync_request_forward"))
{
if (data.startsWith("time_sync_request_forward")) {
let client_time = Number(data.slice("time_sync_request_forward".length + 1));
ws.send(`time_sync_response_forward ${get_time() - client_time}`);
}
if(data.startsWith("state_update_from_client"))
{
if (data.startsWith("state_update_from_client")) {
let new_state = JSON.parse(data.slice("state_update_from_client".length + 1));
let too_soon = (get_time() - state.last_updated) < THRESH_IGNORANCE;
let other_ip = (new_state.client_uid != state.client_uid);
let stale = (new_state.last_updated < state.last_updated)
if (!stale && !(too_soon && other_ip))
{
if (!stale && !(too_soon && other_ip)) {
state = new_state;
wss.clients.forEach(function each(client) {
@ -69,7 +67,9 @@ wss.on('connection', function connection(ws) {
});
app.use('/', express.static(__dirname));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(session({
secret: 'secret key',
@ -85,13 +85,12 @@ app.get("/", function (req, res) {
res.sendFile(__dirname + "/login.html");
});
app.post("/login", function (req, res)
{
app.post("/login", function (req, res) {
const data = req.body;
if(!data)
if (!data)
res.sendStatus(400);
if(data.password == settings.password)
if (data.password == settings.password)
req.session.logged = true;
else
req.session.logged = false;
@ -129,7 +128,10 @@ app.get("/video", function (req, res) {
res.writeHead(206, headers);
// create video read stream for this particular chunk
const videoStream = fs.createReadStream(videoPath, { start, end });
const videoStream = fs.createReadStream(videoPath, {
start,
end
});
// Stream the video chunk to the client
videoStream.pipe(res);
@ -139,7 +141,7 @@ server.listen(settings.server_port, settings.server_ip,
() => console.log(`Server started at ${settings.server_ip}:${settings.server_port}`));
function get_time(){
function get_time() {
let d = new Date();
return d.getTime();
}