From 9432869bce8fcb1967ad850e73afdb653ec00862 Mon Sep 17 00:00:00 2001 From: Unrud Date: Tue, 22 Feb 2022 20:27:39 +0100 Subject: [PATCH] Limit update rate --- main.go | 2 ++ webdata/fn.js | 49 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 8ede20d..913ffde 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,7 @@ const ( ) type config struct { + UpdateRate uint `json:"updateRate"` ScrollSpeed float64 `json:"scrollSpeed"` MoveSpeed float64 `json:"moveSpeed"` MouseScrollSpeed float64 `json:"mouseScrollSpeed"` @@ -156,6 +157,7 @@ func main() { flag.StringVar(&secret, "secret", "", "shared secret for client authentication") flag.StringVar(&certFile, "cert", "", "file containing TLS certificate") flag.StringVar(&keyFile, "key", "", "file containing TLS private key") + flag.UintVar(&config.UpdateRate, "update-rate", 20, "number of updates per second") flag.Float64Var(&config.MoveSpeed, "move-speed", 1, "move speed multiplier") flag.Float64Var(&config.ScrollSpeed, "scroll-speed", 1, "scroll speed multiplier") flag.Float64Var(&config.MouseMoveSpeed, "mouse-move-speed", 1, "mouse move speed multiplier") diff --git a/webdata/fn.js b/webdata/fn.js index acd7e29..8c5664a 100644 --- a/webdata/fn.js +++ b/webdata/fn.js @@ -67,8 +67,10 @@ var scrollXSum = 0; var scrollYSum = 0; var dragging = false; var draggingTimeout = null; +var scrollFinish = false; var scrolling = false; var mouseButtons = 0; +var updateTimeoutActive = false; function fullscreenEnabled() { return (document.fullscreenEnabled || @@ -193,32 +195,51 @@ function onDraggingTimeout() { ws.send("b" + POINTER_BUTTON_LEFT + ";0"); } -function updateMove(x, y) { - moveXSum += x; - moveYSum += y; - var xInt = Math.trunc(moveXSum); - var yInt = Math.trunc(moveYSum); +function startUpdate(fromTimeout) { + if (updateTimeoutActive && !fromTimeout) { + return; + } + var updateFinished = true; + var xInt, yInt; + xInt = Math.trunc(moveXSum); + yInt = Math.trunc(moveYSum); if (xInt != 0 || yInt != 0) { + ws.send("m" + xInt + ";" + yInt); moveXSum -= xInt; moveYSum -= yInt; - ws.send("m" + xInt + ";" + yInt); + updateFinished = false; } -} - -function updateScroll(x, y, scrollFinish) { - scrollXSum += x; - scrollYSum += y; - var xInt = Math.trunc(scrollXSum); - var yInt = Math.trunc(scrollYSum); + xInt = Math.trunc(scrollXSum); + yInt = Math.trunc(scrollYSum); if (xInt != 0 || yInt != 0) { + ws.send((scrollFinish ? "S" : "s") + xInt + ";" + yInt); scrollXSum -= xInt; scrollYSum -= yInt; - ws.send((scrollFinish ? "S" : "s") + xInt + ";" + yInt); scrolling = !scrollFinish; + scrollFinish = false; + updateFinished = false; } else if (scrollFinish && scrolling) { ws.send("S"); scrolling = false; + scrollFinish = false; } + updateTimeoutActive = !updateFinished && config.updateRate > 0 + if (updateTimeoutActive) { + setTimeout(startUpdate, 1000/config.updateRate, true); + } +} + +function updateMove(x, y) { + moveXSum += x; + moveYSum += y; + startUpdate(); +} + +function updateScroll(x, y, finish) { + scrollXSum += x; + scrollYSum += y; + scrollFinish |= finish; + startUpdate(); } function handleTouchstart(evt) {