553f8c60d0ebefbe4042f4a6f55894164b1ea363
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 // Wait for "connect" message to notify connection to the room,
35 // because if game loading is slow the message listener might
36 // not be ready too early.
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 notifyRoom(query
["page"], "connect"); //Hall or Game
45 if (query
["page"].indexOf("/game/") >= 0)
46 notifyRoom("/", "gconnect"); //notify main hall
50 const curPage
= clients
[sid
].page
;
51 socket
.send(JSON
.stringify({code:"pollclients",
52 sockIds: Object
.keys(clients
).filter(k
=>
53 k
!= sid
&& clients
[k
].page
== curPage
58 socket
.send(JSON
.stringify({code:"pollgamers",
59 sockIds: Object
.keys(clients
).filter(k
=>
60 k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0
64 // page change clients[sid].page --> obj.page
65 console
.log(sid
+ " : page change: " + clients
[sid
].page
+ " --> " + obj
.page
);
66 notifyRoom(clients
[sid
].page
, "disconnect");
67 if (clients
[sid
].page
.indexOf("/game/") >= 0)
68 notifyRoom("/", "gdisconnect");
69 clients
[sid
].page
= obj
.page
;
70 notifyRoom(obj
.page
, "connect");
71 if (obj
.page
.indexOf("/game/") >= 0)
72 notifyRoom("/", "gconnect");
75 clients
[obj
.target
].sock
.send(JSON
.stringify(
76 {code:"askidentity",from:sid
}));
79 clients
[obj
.target
].sock
.send(JSON
.stringify(
80 {code:"askchallenge",from:sid
}));
84 // Check all clients playing, and send them a "askgame" message
85 let gameSids
= {}; //game ID --> [sid1, sid2]
86 const regexpGid
= /\/[a-zA-Z0-9]+$/;
87 Object
.keys(clients
).forEach(k
=> {
88 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
90 const gid
= clients
[k
].page
.match(regexpGid
)[0];
94 gameSids
[gid
].push(k
);
97 // Request only one client out of 2 (TODO: this is a bit heavy)
98 // Alt: ask game to all, and filter later?
99 Object
.keys(gameSids
).forEach(gid
=> {
100 const L
= gameSids
[gid
].length
;
102 ? Math
.floor(Math
.random() * Math
.floor(L
))
104 const rid
= gameSids
[gid
][idx
];
105 clients
[rid
].sock
.send(JSON
.stringify(
106 {code:"askgame", from: sid
}));
111 clients
[obj
.target
].sock
.send(JSON
.stringify(
112 {code:"askfullgame", from:sid
}));
115 clients
[obj
.target
].sock
.send(JSON
.stringify(
116 {code:"fullgame", game:obj
.game
}));
119 clients
[obj
.target
].sock
.send(JSON
.stringify(
120 {code:"identity",user:obj
.user
}));
122 case "refusechallenge":
123 clients
[obj
.target
].sock
.send(JSON
.stringify(
124 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
126 case "deletechallenge":
127 clients
[obj
.target
].sock
.send(JSON
.stringify(
128 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
131 clients
[obj
.target
].sock
.send(JSON
.stringify(
132 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
135 clients
[obj
.target
].sock
.send(JSON
.stringify(
136 {code:"challenge", chall:obj
.chall
, from:sid
}));
141 clients
[obj
.target
].sock
.send(JSON
.stringify(
142 {code:"game", game:obj
.game
, from:sid
}));
146 // Notify all room except opponent and me:
147 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
151 notifyRoom(clients
[sid
].page
, "newchat", {chat:obj
.chat
});
153 // TODO: WebRTC instead in this case (most demanding?)
154 // --> At least do a "notifyRoom"
156 clients
[obj
.target
].sock
.send(JSON
.stringify(
157 {code:"newmove", move:obj
.move}));
160 clients
[obj
.target
].sock
.send(JSON
.stringify(
161 {code:"lastate", state:obj
.state
}));
164 clients
[obj
.target
].sock
.send(JSON
.stringify(
165 {code:"resign", side:obj
.side
}));
168 clients
[obj
.target
].sock
.send(JSON
.stringify(
172 clients
[obj
.target
].sock
.send(JSON
.stringify(
173 {code:"drawoffer"}));
176 clients
[obj
.target
].sock
.send(JSON
.stringify(
177 {code:"draw", message:obj
.message
}));
181 socket
.on("close", () => {
182 const page
= clients
[sid
].page
;
184 notifyRoom(page
, "disconnect");
185 if (page
.indexOf("/game/") >= 0)
186 notifyRoom("/", "gdisconnect"); //notify main hall