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
43 console
.log(obj
.code
+ " " + clients
[sid
].page
);
48 notifyRoom(query
["page"], "connect"); //Hall or Game
49 if (query
["page"].indexOf("/game/") >= 0)
50 notifyRoom("/", "gconnect"); //notify main hall
54 const curPage
= clients
[sid
].page
;
55 socket
.send(JSON
.stringify({code:"pollclients",
56 sockIds: Object
.keys(clients
).filter(k
=>
57 k
!= sid
&& clients
[k
].page
== curPage
63 const curPage
= clients
[sid
].page
;
64 socket
.send(JSON
.stringify({code:"pollgamers",
65 sockIds: Object
.keys(clients
).filter(k
=>
66 k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0
71 notifyRoom(clients
[sid
].page
, "disconnect");
72 if (clients
[sid
].page
.indexOf("/game/") >= 0)
73 notifyRoom("/", "gdisconnect");
74 clients
[sid
].page
= obj
.page
;
75 notifyRoom(obj
.page
, "connect");
76 if (obj
.page
.indexOf("/game/") >= 0)
77 notifyRoom("/", "gconnect");
80 clients
[obj
.target
].sock
.send(JSON
.stringify(
81 {code:"askidentity",from:sid
}));
84 clients
[obj
.target
].sock
.send(JSON
.stringify(
85 {code:"askchallenge",from:sid
}));
89 // Check all clients playing, and send them a "askgame" message
90 let gameSids
= {}; //game ID --> [sid1, sid2]
91 const regexpGid
= /\/[a-zA-Z0-9]+$/;
92 Object
.keys(clients
).forEach(k
=> {
93 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
95 const gid
= clients
[k
].page
.match(regexpGid
)[0];
99 gameSids
[gid
].push(k
);
102 // Request only one client out of 2 (TODO: this is a bit heavy)
103 // Alt: ask game to all, and filter later?
104 Object
.keys(gameSids
).forEach(gid
=> {
105 const L
= gameSids
[gid
].length
;
107 ? Math
.floor(Math
.random() * Math
.floor(L
))
109 const rid
= gameSids
[gid
][idx
];
110 clients
[rid
].sock
.send(JSON
.stringify(
111 {code:"askgame", from: sid
}));
116 clients
[obj
.target
].sock
.send(JSON
.stringify(
117 {code:"askfullgame", from:sid
}));
120 clients
[obj
.target
].sock
.send(JSON
.stringify(
121 {code:"fullgame", game:obj
.game
}));
124 clients
[obj
.target
].sock
.send(JSON
.stringify(
125 {code:"identity",user:obj
.user
}));
127 case "refusechallenge":
128 clients
[obj
.target
].sock
.send(JSON
.stringify(
129 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
131 case "deletechallenge":
132 clients
[obj
.target
].sock
.send(JSON
.stringify(
133 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
136 clients
[obj
.target
].sock
.send(JSON
.stringify(
137 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
140 clients
[obj
.target
].sock
.send(JSON
.stringify(
141 {code:"challenge", chall:obj
.chall
, from:sid
}));
146 clients
[obj
.target
].sock
.send(JSON
.stringify(
147 {code:"game", game:obj
.game
, from:sid
}));
151 // Notify all room except opponent and me:
152 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
156 notifyRoom(clients
[sid
].page
, "newchat", {chat:obj
.chat
});
158 // TODO: WebRTC instead in this case (most demanding?)
159 // --> At least do a "notifyRoom"
161 clients
[obj
.target
].sock
.send(JSON
.stringify(
162 {code:"newmove", move:obj
.move}));
165 clients
[obj
.target
].sock
.send(JSON
.stringify(
166 {code:"lastate", state:obj
.state
}));
169 clients
[obj
.target
].sock
.send(JSON
.stringify(
170 {code:"resign", side:obj
.side
}));
173 clients
[obj
.target
].sock
.send(JSON
.stringify(
177 clients
[obj
.target
].sock
.send(JSON
.stringify(
178 {code:"drawoffer"}));
181 clients
[obj
.target
].sock
.send(JSON
.stringify(
182 {code:"draw", message:obj
.message
}));
186 socket
.on("close", () => {
187 const page
= clients
[sid
].page
;
189 notifyRoom(page
, "disconnect");
190 if (page
.indexOf("/game/") >= 0)
191 notifyRoom("/", "gdisconnect"); //notify main hall