diff options
| author | RblSb <msrblsb@gmail.com> | 2020-03-02 14:29:03 +0300 |
|---|---|---|
| committer | RblSb <msrblsb@gmail.com> | 2020-03-02 14:29:03 +0300 |
| commit | b239467d9917849a746f2026c7f0b185a6341914 (patch) | |
| tree | a17a39506d8e8720fced122bca924d8fd6dc9956 /src | |
| parent | 0d36998b0fb8139456bf1eda3f614542fec890c5 (diff) | |
Synch threshold setting
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/Buttons.hx | 18 | ||||
| -rw-r--r-- | src/client/Main.hx | 28 |
2 files changed, 37 insertions, 9 deletions
diff --git a/src/client/Buttons.hx b/src/client/Buttons.hx index f88aa97..d566d68 100644 --- a/src/client/Buttons.hx +++ b/src/client/Buttons.hx @@ -198,6 +198,18 @@ class Buttons { exitBtn.blur(); hideMenus(); } + final synchThresholdBtn = ge("#synchThresholdBtn"); + synchThresholdBtn.onclick = e -> { + var secs = main.synchThreshold + 1; + if (secs > 5) secs = 1; + main.setSynchThreshold(secs); + updateSynchThresholdBtn(main); + synchThresholdBtn.blur(); + } + final text = synchThresholdBtn.innerText; + final secs = main.synchThreshold; + synchThresholdBtn.innerText += ': ${secs}s'; + final swapLayoutBtn = ge("#swapLayoutBtn"); swapLayoutBtn.onclick = e -> { final p = ge("#main"); @@ -227,6 +239,12 @@ class Buttons { for (menu in menus) menu.style.display = ""; } + static function updateSynchThresholdBtn(main:Main):Void { + final text = Lang.get("synchThreshold"); + final secs = main.synchThreshold; + ge("#synchThresholdBtn").innerText = '$text: ${secs}s'; + } + static function initChatInput(main:Main):Void { final guestName:InputElement = cast ge("#guestname"); guestName.onkeydown = e -> { diff --git a/src/client/Main.hx b/src/client/Main.hx index c29604e..5631594 100644 --- a/src/client/Main.hx +++ b/src/client/Main.hx @@ -22,6 +22,7 @@ using ClientTools; class Main { public var isSyncActive = true; + public var synchThreshold(default, null) = 2; final clients:Array<Client> = []; var pageTitle = document.title; final host:String; @@ -32,7 +33,7 @@ class Main { var isConnected = false; var ws:WebSocket; final player:Player; - final onTimeGet = new Timer(2000); + var onTimeGet = new Timer(2000); var onBlinkTab:Null<Timer>; static function main():Void new Main(); @@ -46,11 +47,7 @@ class Main { if (port == "") port = "80"; initListeners(); - onTimeGet.run = () -> { - if (!isSyncActive) return; - if (player.isListEmpty()) return; - send({type: GetTime}); - } + onTimeGet.run = requestTime; document.onvisibilitychange = () -> { if (!document.hidden && onBlinkTab != null) { document.title = getPageTitle(); @@ -63,6 +60,12 @@ class Main { }); } + function requestTime():Void { + if (!isSyncActive) return; + if (player.isListEmpty()) return; + send({type: GetTime}); + } + function openWebSocket(host:String, port:String):Void { ws = new WebSocket('ws://$host:$port'); ws.onmessage = onMessage; @@ -273,19 +276,19 @@ class Main { if (isLeader()) { // if video is loading on leader // move other clients back in time - if (Math.abs(time - newTime) < 2) return; + if (Math.abs(time - newTime) < synchThreshold) return; player.setTime(time, false); return; } if (!data.getTime.paused) player.play(); else player.pause(); - if (Math.abs(time - newTime) < 2) return; + if (Math.abs(time - newTime) < synchThreshold) return; player.setTime(newTime); case SetTime: final newTime = data.setTime.time; final time = player.getTime(); - if (Math.abs(time - newTime) < 2) return; + if (Math.abs(time - newTime) < synchThreshold) return; player.setTime(newTime); case Rewind: @@ -589,6 +592,13 @@ class Main { } } + public function setSynchThreshold(s:Int):Void { + synchThreshold = s; + onTimeGet.stop(); + onTimeGet = new Timer(s * 1000); + onTimeGet.run = requestTime; + } + function escapeRegExp(regex:String):String { return ~/([.*+?^${}()|[\]\\])/g.replace(regex, "\\$1"); } |
