Fix online indicators between Hall and Game pages
[vchess.git] / server / sockets.js
index b3c3a1d..b7689d7 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,13 +33,14 @@ 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)
       {
@@ -55,8 +55,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(
@@ -102,12 +106,12 @@ 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":
@@ -144,6 +148,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
     });
   });
 }