- // Relay (live) game to other player
- break;
- case "newchallenge":
- clients[obj.target].send(JSON.stringify({code:"newchallenge",chall:obj.chall}));
- case "newchat":
- clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg}));
- break;
- // Transmit chats and moves to current room
- // TODO: WebRTC instead in this case (most demanding?)
- case "newmove":
- clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move}));
- break;
- // TODO: generalize that for several opponents
- case "ping":
- socket.send(JSON.stringify({code:"pong",gameId:obj.gameId}));
- break;
- case "lastate":
- const oppId = obj.target;
- obj.oppid = sid; //I'm the opponent of my opponent(s)
- clients[oppId].send(JSON.stringify(obj));
- break;
- // TODO: moreover, here, game info should be sent (through challenge; not stored here)
- case "newgame":
- clients[obj.target].send(JSON.stringify({code:"newgame", game:obj.game}));
- break;
- case "cancelnewgame": //if a user cancel his seek
- // TODO: just transmit event
- //delete games[page];
- break;
- // TODO: also other challenge events
- case "resign":
- clients[obj.target].send(JSON.stringify({code:"resign"}));
- break;
- // TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all
- // also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections
- case "newchallenge":
- console.log("challenge received");
- console.log(obj.sender);
- console.log(obj);
- break;
- }
- });
- socket.on("close", () => {
- delete clients[sid];
- // Notify every other connected client
- Object.keys(clients).forEach( k => {
- clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
- });
- });
- });
+ if (!!obj.target)
+ {
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"game", game:obj.game, from:sid}));
+ }
+ else
+ {
+ // Notify all room except opponent and me:
+ notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
+ }
+ break;
+ case "newchat":
+ notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
+ break;
+ // TODO: WebRTC instead in this case (most demanding?)
+ // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
+ case "newmove":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"newmove", move:obj.move}));
+ break;
+ case "lastate":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"lastate", state:obj.state}));
+ break;
+ case "resign":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"resign", side:obj.side}));
+ break;
+ case "abort":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"abort"}));
+ break;
+ case "drawoffer":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"drawoffer"}));
+ break;
+ case "draw":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"draw", message:obj.message}));
+ break;
+ }
+ };
+ const closeListener = () => {
+ const page = clients[sid].page;
+ delete clients[sid];
+ notifyRoom(page, "disconnect");
+ if (page.indexOf("/game/") >= 0)
+ notifyRoom("/", "gdisconnect"); //notify main hall
+ };
+ if (!!clients[sid])
+ {
+ // Turn off old sock through current client:
+ clients[sid].sock.send(JSON.stringify({code:"duplicate"}));
+ }
+ // Potentially replace current connection:
+ clients[sid] = {sock: socket, page: query["page"]};
+ socket.on("message", messageListener);
+ socket.on("close", closeListener);
+ });