Small fixes
[vchess.git] / server / sockets.js
index ed7275f..ef2f07b 100644 (file)
@@ -17,7 +17,6 @@ module.exports = function(wss) {
   wss.on("connection", (socket, req) => {
     const query = getJsonFromUrl(req.url);
     const sid = query["sid"];
-    // TODO: later, allow duplicate connections (shouldn't be much more complicated)
     if (!!clients[sid])
       return socket.send(JSON.stringify({code:"duplicate"}));
     clients[sid] = {sock: socket, page: query["page"]};
@@ -32,15 +31,20 @@ module.exports = function(wss) {
         }
       });
     };
-    notifyRoom(query["page"], "connect"); //Hall or Game
-    if (query["page"].indexOf("/game/") >= 0)
-      notifyRoom("/", "connect"); //notify main hall
+    // Wait for "connect" message to notify connection to the room,
+    // because if game loading is slow the message listener might
+    // not be ready too early.
     socket.on("message", objtxt => {
       let obj = JSON.parse(objtxt);
       if (!!obj.target && !clients[obj.target])
         return; //receiver not connected, nothing we can do
       switch (obj.code)
       {
+        case "connect":
+          notifyRoom(query["page"], "connect"); //Hall or Game
+          if (query["page"].indexOf("/game/") >= 0)
+            notifyRoom("/", "connect"); //notify main hall
+          break;
         case "pollclients":
           const curPage = clients[sid].page;
           socket.send(JSON.stringify({code:"pollclients",
@@ -151,11 +155,11 @@ module.exports = function(wss) {
           break;
         case "resign":
           clients[obj.target].sock.send(JSON.stringify(
-            {code:"resign"}));
+            {code:"resign", side:obj.side}));
           break;
         case "abort":
           clients[obj.target].sock.send(JSON.stringify(
-            {code:"abort",msg:obj.msg}));
+            {code:"abort"}));
           break;
         case "drawoffer":
           clients[obj.target].sock.send(JSON.stringify(
@@ -163,7 +167,7 @@ module.exports = function(wss) {
           break;
         case "draw":
           clients[obj.target].sock.send(JSON.stringify(
-            {code:"draw"}));
+            {code:"draw", message:obj.message}));
           break;
       }
     });