Fix chat issues when launching a game
[vchess.git] / server / sockets.js
index b3c3a1d..3b16653 100644 (file)
@@ -3,10 +3,9 @@ const url = require('url');
 // Node version in Ubuntu 16.04 does not know about URL class
 function getJsonFromUrl(url)
 {
-  // url: /game/XYZ/?sid=XYZ
-  const queryParts = url.split("?");
-  let result = {page: queryParts[0]};
-  queryParts[1].split("&").forEach((part) => {
+  const query = url.substr(2); //starts with "/?"
+  let result = {};
+  query.split("&").forEach((part) => {
     const item = part.split("=");
     result[item[0]] = decodeURIComponent(item[1]);
   });
@@ -34,14 +33,12 @@ module.exports = function(wss) {
       });
     };
     notifyRoom(query["page"], "connect"); //Hall or Game
+    if (query["page"].indexOf("/game/") >= 0)
+      notifyRoom("/", "connect"); //notify main hall
     socket.on("message", objtxt => {
       let obj = JSON.parse(objtxt);
       if (!!obj.target && !clients[obj.target])
         return; //receiver not connected, nothing we can do
-
-console.log(obj.code);
-console.log(clients);
-
       switch (obj.code)
       {
         case "pollclients":
@@ -55,8 +52,12 @@ console.log(clients);
           break;
         case "pagechange":
           notifyRoom(clients[sid].page, "disconnect");
+          if (clients[sid].page.indexOf("/game/") >= 0)
+            notifyRoom("/", "disconnect");
           clients[sid].page = obj.page;
           notifyRoom(obj.page, "connect");
+          if (obj.page.indexOf("/game/") >= 0)
+            notifyRoom("/", "connect");
           break;
         case "askidentity":
           clients[obj.target].sock.send(JSON.stringify(
@@ -66,7 +67,7 @@ console.log(clients);
           clients[obj.target].sock.send(JSON.stringify(
             {code:"askchallenge",from:sid}));
           break;
-        case "askgame":
+        case "askgames":
           // Check all clients playing, and send them a "askgame" message
           Object.keys(clients).forEach(k => {
             if (k != sid && clients[k].page.indexOf("/game/") >= 0)
@@ -75,8 +76,6 @@ console.log(clients);
                 {code:"askgame", from: sid}));
             }
           });
-          clients[obj.target].sock.send(JSON.stringify(
-            {code:"askgame",from:sid}));
           break;
         case "identity":
           clients[obj.target].sock.send(JSON.stringify(
@@ -102,16 +101,18 @@ console.log(clients);
           if (!!obj.target)
           {
             clients[obj.target].sock.send(JSON.stringify(
-              {code:"game", game:data.game, from:sid}));
+              {code:"game", game:obj.game, from:sid}));
           }
           else
           {
             // Notify all room except opponent and me:
-            notifyRoom("/", "game", {game:data.game}, [data.oppsid]);
+            notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
           }
           break;
         case "newchat":
-          notifyRoom(query["page"], "newchat", {msg:obj.msg, name:obj.name});
+          // WARNING: do not use query["page"], because the page may change
+          notifyRoom(clients[sid].page, "newchat",
+            {msg: obj.msg, name: obj.name});
           break;
         // TODO: WebRTC instead in this case (most demanding?)
         case "newmove":
@@ -144,6 +145,8 @@ console.log(clients);
       const page = clients[sid].page;
       delete clients[sid];
       notifyRoom(page, "disconnect");
+      if (page.indexOf("/game/") >= 0)
+        notifyRoom("/", "disconnect"); //notify main hall
     });
   });
 }