e4aa4493c15bad223613509d12a4bbf1de37127b
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 module
.exports = function(wss
) {
16 let clients
= {}; //associative array sid --> socket
17 wss
.on("connection", (socket
, req
) => {
18 const query
= getJsonFromUrl(req
.url
);
19 const sid
= query
["sid"];
21 return socket
.send(JSON
.stringify({code:"duplicate"}));
22 clients
[sid
] = {sock: socket
, page: query
["page"]};
23 const notifyRoom
= (page
,code
,obj
={},excluded
=[]) => {
24 Object
.keys(clients
).forEach(k
=> {
27 if (k
!= sid
&& clients
[k
].page
== page
)
29 clients
[k
].sock
.send(JSON
.stringify(Object
.assign(
30 {code:code
, from:sid
}, obj
)));
34 notifyRoom(query
["page"], "connect"); //Hall or Game
35 if (query
["page"].indexOf("/game/") >= 0)
36 notifyRoom("/", "connect"); //notify main hall
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
44 const curPage
= clients
[sid
].page
;
45 socket
.send(JSON
.stringify({code:"pollclients",
46 sockIds: Object
.keys(clients
).filter(k
=> k
!= sid
&&
47 (clients
[k
].page
== curPage
||
48 // Consider that people playing are in Hall too:
49 (curPage
== "/" && clients
[k
].page
.indexOf("/game/") >= 0))
53 notifyRoom(clients
[sid
].page
, "disconnect");
54 if (clients
[sid
].page
.indexOf("/game/") >= 0)
55 notifyRoom("/", "disconnect");
56 clients
[sid
].page
= obj
.page
;
57 notifyRoom(obj
.page
, "connect");
58 if (obj
.page
.indexOf("/game/") >= 0)
59 notifyRoom("/", "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
}));
71 // Check all clients playing, and send them a "askgame" message
72 let gameSids
= {}; //game ID --> [sid1, sid2]
73 const regexpGid
= /\/[a-zA-Z0-9]+$/;
74 Object
.keys(clients
).forEach(k
=> {
75 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
77 const gid
= clients
[k
].page
.match(regexpGid
)[0];
81 gameSids
[gid
].push(k
);
84 // Request only one client out of 2 (TODO: this is a bit heavy)
85 // Alt: ask game to all, and filter later?
86 Object
.keys(gameSids
).forEach(gid
=> {
87 const L
= gameSids
[gid
].length
;
89 ? Math
.floor(Math
.random() * Math
.floor(L
))
91 const rid
= gameSids
[gid
][idx
];
92 clients
[rid
].sock
.send(JSON
.stringify(
93 {code:"askgame", from: sid
}));
98 clients
[obj
.target
].sock
.send(JSON
.stringify(
99 {code:"askfullgame", from:sid
}));
102 clients
[obj
.target
].sock
.send(JSON
.stringify(
103 {code:"fullgame", game:obj
.game
}));
106 clients
[obj
.target
].sock
.send(JSON
.stringify(
107 {code:"identity",user:obj
.user
}));
109 case "refusechallenge":
110 clients
[obj
.target
].sock
.send(JSON
.stringify(
111 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
113 case "deletechallenge":
114 clients
[obj
.target
].sock
.send(JSON
.stringify(
115 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
118 clients
[obj
.target
].sock
.send(JSON
.stringify(
119 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
122 clients
[obj
.target
].sock
.send(JSON
.stringify(
123 {code:"challenge", chall:obj
.chall
, from:sid
}));
128 clients
[obj
.target
].sock
.send(JSON
.stringify(
129 {code:"game", game:obj
.game
, from:sid
}));
133 // Notify all room except opponent and me:
134 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
138 // WARNING: do not use query["page"], because the page may change
139 notifyRoom(clients
[sid
].page
, "newchat",
140 {msg: obj
.msg
, name: obj
.name
});
142 // TODO: WebRTC instead in this case (most demanding?)
144 clients
[obj
.target
].sock
.send(JSON
.stringify(
145 {code:"newmove", move:obj
.move}));
148 clients
[obj
.target
].sock
.send(JSON
.stringify(
149 {code:"lastate", state:obj
.state
}));
152 clients
[obj
.target
].sock
.send(JSON
.stringify(
156 clients
[obj
.target
].sock
.send(JSON
.stringify(
157 {code:"abort",msg:obj
.msg
}));
160 clients
[obj
.target
].sock
.send(JSON
.stringify(
161 {code:"drawoffer"}));
164 clients
[obj
.target
].sock
.send(JSON
.stringify(
169 socket
.on("close", () => {
170 const page
= clients
[sid
].page
;
172 notifyRoom(page
, "disconnect");
173 if (page
.indexOf("/game/") >= 0)
174 notifyRoom("/", "disconnect"); //notify main hall