1 const url
= require('url');
3 // Node version in Ubuntu 16.04 does not know about URL class
4 function getJsonFromUrl(url
)
6 const query
= url
.substr(2); //starts with "/?"
8 query
.split("&").forEach((part
) => {
9 const item
= part
.split("=");
10 result
[item
[0]] = decodeURIComponent(item
[1]);
15 // Removal in array of strings (socket IDs)
16 function remInArray(arr
, item
)
18 const idx
= arr
.indexOf(item
);
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
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 // Ignore duplicate connections (on the same live game that we play):
36 return socket
.send(JSON
.stringify({code:"duplicate"}));
37 clients
[sid
] = socket
;
39 Object
.keys(clients
).forEach(k
=> {
41 clients
[k
].send(JSON
.stringify({code:"connect",sid:sid
}));
43 socket
.on("message", objtxt
=> {
44 let obj
= JSON
.parse(objtxt
);
45 if (!!obj
.target
&& !clients
[obj
.target
])
46 return; //receiver not connected, nothing we can do
47 //console.log(obj.code);
51 socket
.send(JSON
.stringify({code:"pollclients",
52 sockIds:Object
.keys(clients
).filter(k
=> k
!= sid
)}));
55 clients
[obj
.target
].send(
56 JSON
.stringify({code:"askidentity",from:sid
}));
59 clients
[obj
.target
].send(
60 JSON
.stringify({code:"askchallenge",from:sid
}));
63 clients
[obj
.target
].send(
64 JSON
.stringify({code:"askgame",from:sid
}));
67 clients
[obj
.target
].send(
68 JSON
.stringify({code:"identity",user:obj
.user
}));
71 clients
[obj
.target
].send(
72 JSON
.stringify({code:"challenge", chall:obj
.chall
, from:sid
}));
74 case "acceptchallenge":
75 clients
[obj
.target
].send(
76 JSON
.stringify({code:"acceptchallenge", cid:obj
.cid
, from:sid
}));
78 case "withdrawchallenge":
79 clients
[obj
.target
].send(
80 JSON
.stringify({code:"withdrawchallenge", cid:obj
.cid
, from:sid
}));
82 case "refusechallenge":
83 clients
[obj
.target
].send(
84 JSON
.stringify({code:"refusechallenge", cid:obj
.cid
, from:sid
}));
86 case "deletechallenge":
87 clients
[obj
.target
].send(
88 JSON
.stringify({code:"deletechallenge", cid:obj
.cid
, from:sid
}));
91 clients
[obj
.target
].send(JSON
.stringify(
92 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
95 // TODO: relay (live) game to other player
98 clients
[obj
.target
].send(JSON
.stringify({code:"newchat",msg:obj
.msg
}));
100 // Transmit chats and moves to current room
101 // TODO: WebRTC instead in this case (most demanding?)
103 clients
[obj
.target
].send(JSON
.stringify({code:"newmove",move:obj
.move}));
105 // TODO: generalize that for several opponents
107 socket
.send(JSON
.stringify({code:"pong",gameId:obj
.gameId
}));
110 const oppId
= obj
.target
;
111 obj
.oppid
= sid
; //I'm the opponent of my opponent(s)
112 clients
[oppId
].send(JSON
.stringify(obj
));
115 clients
[obj
.target
].send(JSON
.stringify({code:"resign"}));
119 socket
.on("close", () => {
121 // Notify every other connected client
122 Object
.keys(clients
).forEach( k
=> {
123 clients
[k
].send(JSON
.stringify({code:"disconnect",sid:sid
}));