Skip to content

Commit

Permalink
Merge pull request #4 from Triadica/disable-raf
Browse files Browse the repository at this point in the history
expose disable-raf option
  • Loading branch information
NoEgAm authored Oct 19, 2023
2 parents bf6f521 + 598b172 commit 297092f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);

Expand All @@ -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);
Expand Down
45 changes: 25 additions & 20 deletions src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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__;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 297092f

Please sign in to comment.