Fix chat issues when launching a game
[vchess.git] / server / sockets.js
index b7689d7..3b16653 100644 (file)
@@ -39,9 +39,6 @@ module.exports = function(wss) {
       let obj = JSON.parse(objtxt);
       if (!!obj.target && !clients[obj.target])
         return; //receiver not connected, nothing we can do
-
-console.log(obj.code);
-
       switch (obj.code)
       {
         case "pollclients":
@@ -70,7 +67,7 @@ console.log(obj.code);
           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)
@@ -79,8 +76,6 @@ console.log(obj.code);
                 {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(
@@ -115,7 +110,9 @@ console.log(obj.code);
           }
           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":