diff --git a/package.json b/package.json index a84e7ca..ec5685c 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "rcontrol": "./server.mjs" }, "devDependencies": { - "@triadica/touch-control": "^0.0.3", + "@triadica/touch-control": "^0.0.4-a1", "@types/ws": "^8.5.5", "git-describe": "^4.1.1", "query-string": "^8.1.0", diff --git a/server.mjs b/server.mjs index 39c39c9..108a232 100755 --- a/server.mjs +++ b/server.mjs @@ -4,8 +4,12 @@ import { WebSocketServer } from "ws"; let port = parseInt(process.env.PORT || "6200"); +let verbose = process.env.verbose; + const wss = new WebSocketServer({ port, host: "0.0.0.0" }); +let lastTime = Date.now(); + wss.on("connection", function connection(ws) { console.log(`new connection. (${wss.clients.size})`); @@ -19,7 +23,11 @@ wss.on("connection", function connection(ws) { ws.controlSender = true; console.log("sender declared"); } else if (op.action === "control" || op.action === "button") { - // console.log("control clients:", wss.clients.size); + let now = Date.now(); + if (verbose === "true") { + console.debug("control time delta:", now - lastTime); + } + lastTime = now; wss.clients.forEach((client) => { if (client !== ws && !client.controlSender) { client.send(s); diff --git a/src/main.mts b/src/main.mts index 8a790b3..00a1013 100644 --- a/src/main.mts +++ b/src/main.mts @@ -9,6 +9,7 @@ const parsed = queryString.parse(location.search); let host = (parsed.host as string) || location.hostname; let port = parseInt((parsed.port as string) || "6200"); +let disableRaf = parsed["disable-raf"] === "true"; let connected = false; @@ -72,27 +73,31 @@ let allZero = (xs: number[]): boolean => { let main = () => { connect(); renderControl(); - startControlLoop(10, (elapsed, states, delta) => { - showData(elapsed, states, delta); - if (connected) { - if ( - allZero(delta.leftMove) && - allZero(delta.rightMove) && - allZero(states.leftMove) && - allZero(states.rightMove) - ) { - return; + startControlLoop( + 10, + (elapsed, states, delta) => { + showData(elapsed, states, delta); + if (connected) { + if ( + allZero(delta.leftMove) && + allZero(delta.rightMove) && + allZero(states.leftMove) && + allZero(states.rightMove) + ) { + return; + } + ws.send( + JSON.stringify({ + action: "control", + elapsed, + states, + delta, + }) + ); } - ws.send( - JSON.stringify({ - action: "control", - elapsed, - states, - delta, - }) - ); - } - }); + }, + { disableRaf } + ); console.log("hash:", __COMMIT_HASH__); document.querySelector(".hash")!.innerHTML = __COMMIT_HASH__; diff --git a/yarn.lock b/yarn.lock index e6d64cf..1f07483 100644 --- a/yarn.lock +++ b/yarn.lock @@ -112,10 +112,10 @@ resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.17.19.tgz#8cfaf2ff603e9aabb910e9c0558c26cf32744061" integrity sha512-lAhycmKnVOuRYNtRtatQR1LPQf2oYCkRGkSFnseDAKPl8lu5SOsK/e1sXe5a0Pc5kHIHe6P2I/ilntNv2xf3cA== -"@triadica/touch-control@^0.0.3": - version "0.0.3" - resolved "https://registry.yarnpkg.com/@triadica/touch-control/-/touch-control-0.0.3.tgz#91e7ecb8fd2a36309180eada58f843b1dd44d876" - integrity sha512-P6E+MJYmN6EJim0MjhK2kf99OjTZJwAXI7clJz6DDsaf/HCIbyYnVPWyMC7jgc6bPG2I5PAnLga5gHTR1lbUzA== +"@triadica/touch-control@^0.0.4-a1": + version "0.0.4-a1" + resolved "https://registry.yarnpkg.com/@triadica/touch-control/-/touch-control-0.0.4-a1.tgz#11aa2b1216d54f62920f7524b296f403cd8d20df" + integrity sha512-L+Qf3UTWhoGcB/iYXcOJ0hfknMNCHOOB+cWkkmfdg6JJ79cm0kjLA821a4N3ciR4wH7G56U13hZTsyd/TifAuQ== "@types/node@*": version "20.3.1"