1 const url
= require('url');
3 // Node version in Ubuntu 16.04 does not know about URL class
4 function getJsonFromUrl(url
)
6 // url: /game/XYZ/?sid=XYZ
7 const queryParts
= url
.split("?");
8 let result
= {page: queryParts
[0]};
9 queryParts
[1].split("&").forEach((part
) => {
10 const item
= part
.split("=");
11 result
[item
[0]] = decodeURIComponent(item
[1]);
16 module
.exports = function(wss
) {
17 let clients
= {}; //associative array sid --> socket
18 wss
.on("connection", (socket
, req
) => {
19 const query
= getJsonFromUrl(req
.url
);
20 const sid
= query
["sid"];
21 // TODO: later, allow duplicate connections (shouldn't be much more complicated)
23 return socket
.send(JSON
.stringify({code:"duplicate"}));
24 clients
[sid
] = {sock: socket
, page: query
["page"]};
25 const notifyRoom
= (page
,code
,obj
={},excluded
=[]) => {
26 Object
.keys(clients
).forEach(k
=> {
29 if (k
!= sid
&& clients
[k
].page
== page
)
31 clients
[k
].sock
.send(JSON
.stringify(Object
.assign(
32 {code:code
, from:sid
}, obj
)));
36 notifyRoom(query
["page"], "connect"); //Hall or Game
37 socket
.on("message", objtxt
=> {
38 let obj
= JSON
.parse(objtxt
);
39 if (!!obj
.target
&& !clients
[obj
.target
])
40 return; //receiver not connected, nothing we can do
42 console
.log(obj
.code
);
48 const curPage
= clients
[sid
].page
;
49 socket
.send(JSON
.stringify({code:"pollclients",
50 sockIds: Object
.keys(clients
).filter(k
=> k
!= sid
&&
51 (clients
[k
].page
== curPage
||
52 // Consider that people playing are in Hall too:
53 (curPage
== "/" && clients
[k
].page
.indexOf("/game/") >= 0))
57 notifyRoom(clients
[sid
].page
, "disconnect");
58 clients
[sid
].page
= obj
.page
;
59 notifyRoom(obj
.page
, "connect");
62 clients
[obj
.target
].sock
.send(JSON
.stringify(
63 {code:"askidentity",from:sid
}));
66 clients
[obj
.target
].sock
.send(JSON
.stringify(
67 {code:"askchallenge",from:sid
}));
70 // Check all clients playing, and send them a "askgame" message
71 Object
.keys(clients
).forEach(k
=> {
72 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
74 clients
[k
].sock
.send(JSON
.stringify(
75 {code:"askgame", from: sid
}));
78 clients
[obj
.target
].sock
.send(JSON
.stringify(
79 {code:"askgame",from:sid
}));
82 clients
[obj
.target
].sock
.send(JSON
.stringify(
83 {code:"identity",user:obj
.user
}));
85 case "refusechallenge":
86 clients
[obj
.target
].sock
.send(JSON
.stringify(
87 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
89 case "deletechallenge":
90 clients
[obj
.target
].sock
.send(JSON
.stringify(
91 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
94 clients
[obj
.target
].sock
.send(JSON
.stringify(
95 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
98 clients
[obj
.target
].sock
.send(JSON
.stringify(
99 {code:"challenge", chall:obj
.chall
, from:sid
}));
104 clients
[obj
.target
].sock
.send(JSON
.stringify(
105 {code:"game", game:data
.game
, from:sid
}));
109 // Notify all room except opponent and me:
110 notifyRoom("/", "game", {game:data
.game
}, [data
.oppsid
]);
114 notifyRoom(query
["page"], "newchat", {msg:obj
.msg
, name:obj
.name
});
116 // TODO: WebRTC instead in this case (most demanding?)
118 clients
[obj
.target
].sock
.send(JSON
.stringify(
119 {code:"newmove", move:obj
.move}));
122 clients
[obj
.target
].sock
.send(JSON
.stringify(
123 {code:"lastate", state:obj
.state
}));
126 clients
[obj
.target
].sock
.send(JSON
.stringify(
130 clients
[obj
.target
].sock
.send(JSON
.stringify(
131 {code:"abort",msg:obj
.msg
}));
134 clients
[obj
.target
].sock
.send(JSON
.stringify(
135 {code:"drawoffer"}));
138 clients
[obj
.target
].sock
.send(JSON
.stringify(
143 socket
.on("close", () => {
144 const page
= clients
[sid
].page
;
146 notifyRoom(page
, "disconnect");