From d923ed046d8caa9fe8cdb057ce780f6b481824ba Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Thu, 13 Jul 2023 22:53:57 +0000 Subject: [PATCH] change vars.js.template to tempalte.vars.js, implement interrupt based client sync --- README.md | 2 +- scripts/clientsync.js | 19 +++++++++++++++++-- vars.js.template => template.vars.js | 0 3 files changed, 18 insertions(+), 3 deletions(-) rename vars.js.template => template.vars.js (100%) diff --git a/README.md b/README.md index 6de0565..22375d7 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ After this step, the Client should be available on the `Client Host` at port `80 ## Configuration - Client 1. In `Client Host`, navigate to this repo's root folder -2. Copy `vars.js.template` to `vars.js` and assign the `API` variable with the value of the API's URL. This will likely be `client./api` +2. Rename `tempalte.vars.js` to `vars.js` and assign the `API` variable with the value of the API's URL. This will likely be `client./api` ## Installation - API diff --git a/scripts/clientsync.js b/scripts/clientsync.js index 253dd17..7c604fe 100644 --- a/scripts/clientsync.js +++ b/scripts/clientsync.js @@ -1,4 +1,5 @@ import { requestAPI } from "./utils.js"; +import { API } from "../vars.js"; export async function setupClientSync (callback) { let scheme = localStorage.getItem("sync-scheme"); @@ -29,9 +30,23 @@ export async function setupClientSync (callback) { }, rate * 1000); } else if (scheme === "interrupt") { - + callback(); + const socket = new WebSocket(`wss://${API.replace("https://", "")}/sync/interrupt`); + socket.addEventListener("open", (event) => { + socket.send(`rate ${rate}`); + }); + socket.addEventListener("message", (event) => { + let message = event.data.toString(); + if (message === "sync") { + callback(); + } + else { + console.error("clientsync: recieved unexpected message from server, closing socket.") + socket.close(); + } + }); } else { - + console.error(`clientsync: unsupported scheme ${scheme} selected.`) } } \ No newline at end of file diff --git a/vars.js.template b/template.vars.js similarity index 100% rename from vars.js.template rename to template.vars.js