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
45 const curPage
= clients
[sid
].page
;
46 notifyRoom(curPage
, "connect"); //Hall or Game
47 if (curPage
.indexOf("/game/") >= 0)
48 notifyRoom("/", "gconnect"); //notify main hall
53 const curPage
= clients
[sid
].page
;
54 socket
.send(JSON
.stringify({code:"pollclients",
55 sockIds: Object
.keys(clients
).filter(k
=>
56 k
!= sid
&& clients
[k
].page
== curPage
61 socket
.send(JSON
.stringify({code:"pollgamers",
62 sockIds: Object
.keys(clients
).filter(k
=>
63 k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0
67 // page change clients[sid].page --> obj.page
68 notifyRoom(clients
[sid
].page
, "disconnect");
69 if (clients
[sid
].page
.indexOf("/game/") >= 0)
70 notifyRoom("/", "gdisconnect");
71 clients
[sid
].page
= obj
.page
;
72 notifyRoom(obj
.page
, "connect");
73 if (obj
.page
.indexOf("/game/") >= 0)
74 notifyRoom("/", "gconnect");
77 clients
[obj
.target
].sock
.send(JSON
.stringify(
78 {code:"askidentity",from:sid
}));
81 clients
[obj
.target
].sock
.send(JSON
.stringify(
82 {code:"askchallenge",from:sid
}));
86 // Check all clients playing, and send them a "askgame" message
87 let gameSids
= {}; //game ID --> [sid1, sid2]
88 const regexpGid
= /\/[a-zA-Z0-9]+$/;
89 Object
.keys(clients
).forEach(k
=> {
90 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
92 const gid
= clients
[k
].page
.match(regexpGid
)[0];
96 gameSids
[gid
].push(k
);
99 // Request only one client out of 2 (TODO: this is a bit heavy)
100 // Alt: ask game to all, and filter later?
101 Object
.keys(gameSids
).forEach(gid
=> {
102 const L
= gameSids
[gid
].length
;
104 ? Math
.floor(Math
.random() * Math
.floor(L
))
106 const rid
= gameSids
[gid
][idx
];
107 clients
[rid
].sock
.send(JSON
.stringify(
108 {code:"askgame", from: sid
}));
113 clients
[obj
.target
].sock
.send(JSON
.stringify(
114 {code:"askfullgame", from:sid
}));
117 clients
[obj
.target
].sock
.send(JSON
.stringify(
118 {code:"fullgame", game:obj
.game
}));
121 clients
[obj
.target
].sock
.send(JSON
.stringify(
122 {code:"identity",user:obj
.user
}));
124 case "refusechallenge":
125 clients
[obj
.target
].sock
.send(JSON
.stringify(
126 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
128 case "deletechallenge":
129 clients
[obj
.target
].sock
.send(JSON
.stringify(
130 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
133 clients
[obj
.target
].sock
.send(JSON
.stringify(
134 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
137 clients
[obj
.target
].sock
.send(JSON
.stringify(
138 {code:"challenge", chall:obj
.chall
, from:sid
}));
143 clients
[obj
.target
].sock
.send(JSON
.stringify(
144 {code:"game", game:obj
.game
, from:sid
}));
148 // Notify all room except opponent and me:
149 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
153 notifyRoom(clients
[sid
].page
, "newchat", {chat:obj
.chat
});
155 // TODO: WebRTC instead in this case (most demanding?)
156 // --> At least do a "notifyRoom"
158 clients
[obj
.target
].sock
.send(JSON
.stringify(
159 {code:"newmove", move:obj
.move}));
162 clients
[obj
.target
].sock
.send(JSON
.stringify(
163 {code:"lastate", state:obj
.state
}));
166 clients
[obj
.target
].sock
.send(JSON
.stringify(
167 {code:"resign", side:obj
.side
}));
170 clients
[obj
.target
].sock
.send(JSON
.stringify(
174 clients
[obj
.target
].sock
.send(JSON
.stringify(
175 {code:"drawoffer"}));
178 clients
[obj
.target
].sock
.send(JSON
.stringify(
179 {code:"draw", message:obj
.message
}));
183 socket
.on("close", () => {
184 const page
= clients
[sid
].page
;
186 notifyRoom(page
, "disconnect");
187 if (page
.indexOf("/game/") >= 0)
188 notifyRoom("/", "gdisconnect"); //notify main hall