| 1 | const url = require('url'); |
| 2 | |
| 3 | // Node version in Ubuntu 16.04 does not know about URL class |
| 4 | function getJsonFromUrl(url) |
| 5 | { |
| 6 | const query = url.substr(2); //starts with "/?" |
| 7 | let result = {}; |
| 8 | query.split("&").forEach((part) => { |
| 9 | const item = part.split("="); |
| 10 | result[item[0]] = decodeURIComponent(item[1]); |
| 11 | }); |
| 12 | return result; |
| 13 | } |
| 14 | |
| 15 | // Removal in array of strings (socket IDs) |
| 16 | function remInArray(arr, item) |
| 17 | { |
| 18 | const idx = arr.indexOf(item); |
| 19 | if (idx >= 0) |
| 20 | arr.splice(idx, 1); |
| 21 | } |
| 22 | |
| 23 | // TODO: empêcher multi-log du même user (envoyer le user ID + secret en même temps que name et...) |
| 24 | // --> si secret ne matche pas celui trouvé en DB, stop |
| 25 | // TODO: lorsque challenge accepté, seul le dernier joueur à accepter envoi message "please start game" |
| 26 | // avec les coordonnées des participants. Le serveur renvoit alors les détails de la partie (couleurs, position) |
| 27 | //TODO: programmatic re-navigation on current game if we receive a move and are not there |
| 28 | |
| 29 | module.exports = function(wss) { |
| 30 | let clients = {}; //associative array sid --> socket |
| 31 | wss.on("connection", (socket, req) => { |
| 32 | const query = getJsonFromUrl(req.url); |
| 33 | const sid = query["sid"]; |
| 34 | // TODO: later, allow duplicate connections (shouldn't be much more complicated) |
| 35 | // Ignore duplicate connections (on the same live game that we play): |
| 36 | if (!!clients[sid]) |
| 37 | return socket.send(JSON.stringify({code:"duplicate"})); |
| 38 | clients[sid] = socket; |
| 39 | // Notify room: |
| 40 | Object.keys(clients).forEach(k => { |
| 41 | if (k != sid) |
| 42 | clients[k].send(JSON.stringify({code:"connect",sid:sid})); |
| 43 | }); |
| 44 | socket.on("message", objtxt => { |
| 45 | let obj = JSON.parse(objtxt); |
| 46 | if (!!obj.target && !clients[obj.target]) |
| 47 | return; //receiver not connected, nothing we can do |
| 48 | //console.log(obj.code); |
| 49 | switch (obj.code) |
| 50 | { |
| 51 | case "pollclients": |
| 52 | socket.send(JSON.stringify({code:"pollclients", |
| 53 | sockIds:Object.keys(clients).filter(k => k != sid)})); |
| 54 | break; |
| 55 | case "askidentity": |
| 56 | clients[obj.target].send( |
| 57 | JSON.stringify({code:"askidentity",from:sid})); |
| 58 | break; |
| 59 | case "askchallenge": |
| 60 | clients[obj.target].send( |
| 61 | JSON.stringify({code:"askchallenge",from:sid})); |
| 62 | break; |
| 63 | case "askgame": |
| 64 | clients[obj.target].send( |
| 65 | JSON.stringify({code:"askgame",from:sid})); |
| 66 | break; |
| 67 | case "identity": |
| 68 | clients[obj.target].send( |
| 69 | JSON.stringify({code:"identity",user:obj.user})); |
| 70 | break; |
| 71 | case "challenge": |
| 72 | clients[obj.target].send( |
| 73 | JSON.stringify({code:"challenge", chall:obj.chall, from:sid})); |
| 74 | break; |
| 75 | case "acceptchallenge": |
| 76 | clients[obj.target].send( |
| 77 | JSON.stringify({code:"acceptchallenge", cid:obj.cid, from:sid})); |
| 78 | break; |
| 79 | case "withdrawchallenge": |
| 80 | clients[obj.target].send( |
| 81 | JSON.stringify({code:"withdrawchallenge", cid:obj.cid, from:sid})); |
| 82 | break; |
| 83 | case "refusechallenge": |
| 84 | clients[obj.target].send( |
| 85 | JSON.stringify({code:"refusechallenge", cid:obj.cid, from:sid})); |
| 86 | break; |
| 87 | case "deletechallenge": |
| 88 | clients[obj.target].send( |
| 89 | JSON.stringify({code:"deletechallenge", cid:obj.cid, from:sid})); |
| 90 | break; |
| 91 | case "newgame": |
| 92 | clients[obj.target].send(JSON.stringify( |
| 93 | {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid})); |
| 94 | break; |
| 95 | case "game": |
| 96 | // TODO: relay (live) game to other player |
| 97 | break; |
| 98 | case "newchat": |
| 99 | clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg})); |
| 100 | break; |
| 101 | // Transmit chats and moves to current room |
| 102 | // TODO: WebRTC instead in this case (most demanding?) |
| 103 | case "newmove": |
| 104 | clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move})); |
| 105 | break; |
| 106 | // TODO: generalize that for several opponents |
| 107 | case "ping": |
| 108 | socket.send(JSON.stringify({code:"pong",gameId:obj.gameId})); |
| 109 | break; |
| 110 | case "lastate": |
| 111 | const oppId = obj.target; |
| 112 | obj.oppid = sid; //I'm the opponent of my opponent(s) |
| 113 | clients[oppId].send(JSON.stringify(obj)); |
| 114 | break; |
| 115 | case "resign": |
| 116 | clients[obj.target].send(JSON.stringify({code:"resign"})); |
| 117 | break; |
| 118 | case "abort": |
| 119 | clients[obj.target].send(JSON.stringify({code:"abort",msg:obj.msg})); |
| 120 | break; |
| 121 | } |
| 122 | }); |
| 123 | socket.on("close", () => { |
| 124 | delete clients[sid]; |
| 125 | // Notify every other connected client |
| 126 | Object.keys(clients).forEach( k => { |
| 127 | clients[k].send(JSON.stringify({code:"disconnect",sid:sid})); |
| 128 | }); |
| 129 | }); |
| 130 | }); |
| 131 | } |