From: Raphaƫl Jakse Date: Tue, 28 Apr 2020 17:29:15 +0000 (+0200) Subject: Block actions until responses are received with WebSockets X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=2fc231dfec0eb2dc861c8bb580936236e1a47728;p=trivabble.git Block actions until responses are received with WebSockets --- diff --git a/trivabble.js b/trivabble.js index 022b92d..eecd073 100644 --- a/trivabble.js +++ b/trivabble.js @@ -88,6 +88,9 @@ let pollingServer = false; let connectionStopped = false; + let currentMessageId = 1; + const waitingMsgs = []; + let serverVersion = 0; // HTTP path url prefix for any socket (xhr,ess or ws) from browser to server access @@ -388,7 +391,7 @@ function dragTileBegin(e) { preventDefault(e); - if (blockMove) { + if (blockMove || waitingMsgs.length) { return; } @@ -678,6 +681,13 @@ return; } + if (data.id) { + const pos = waitingMsgs.indexOf(data.id); + if (pos !== -1) { + waitingMsgs.splice(pos, 1); + } + } + if (data.version) { serverVersion = data.version; } @@ -1142,6 +1152,13 @@ function sendCmds(cmds) { if (webSocket && webSocket.readyState === 1) { + if (serverVersion >= 202004281800) { + for (const cmd of cmds) { + waitingMsgs.push(currentMessageId); + cmd.id = currentMessageId++; + } + } + webSocket.send(JSON.stringify(cmds)); return; } @@ -1229,7 +1246,7 @@ function bagClicked(e) { preventDefault(e); - if (blockMove) { + if (blockMove || waitingMsgs.length) { return; }