Block actions until responses are received with WebSockets
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Tue, 28 Apr 2020 17:29:15 +0000 (19:29 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Tue, 28 Apr 2020 17:29:15 +0000 (19:29 +0200)
trivabble.js

index 022b92d50571593af6900ecf16a260167ffbd2ad..eecd0736f77d5461455d7bcd8f20c5f72fe6711f 100644 (file)
@@ -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
     function dragTileBegin(e) {
         preventDefault(e);
 
-        if (blockMove) {
+        if (blockMove || waitingMsgs.length) {
             return;
         }
 
             return;
         }
 
+        if (data.id) {
+            const pos = waitingMsgs.indexOf(data.id);
+            if (pos !== -1) {
+                waitingMsgs.splice(pos, 1);
+            }
+        }
+
         if (data.version) {
             serverVersion = data.version;
         }
 
     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;
         }
     function bagClicked(e) {
         preventDefault(e);
 
-        if (blockMove) {
+        if (blockMove || waitingMsgs.length) {
             return;
         }