64b180dbb054d388f506137c8775e50d9a2c04f8
1 const url
= require('url');
3 // Node version in Ubuntu 16.04 does not know about URL class
4 // NOTE: url is already transformed, without ?xxx=yyy... parts
5 function getJsonFromUrl(url
)
7 const query
= url
.substr(2); //starts with "/?"
9 query
.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 if (query
["page"] != "/" && query
["page"].indexOf("/game/") < 0)
21 return; //other tabs don't need to be connected
22 const sid
= query
["sid"];
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 const messageListener
= (objtxt
) => {
35 let obj
= JSON
.parse(objtxt
);
36 if (!!obj
.target
&& !clients
[obj
.target
])
37 return; //receiver not connected, nothing we can do
41 // Turn off message listening, and send disconnect if needed:
42 socket
.removeListener("message", messageListener
);
43 socket
.removeListener("close", closeListener
);
44 // From obj.page to clients[sid].page (TODO: unclear)
45 if (clients
[sid
].page
!= obj
.page
)
47 notifyRoom(obj
.page
, "disconnect");
48 if (obj
.page
.indexOf("/game/") >= 0)
49 notifyRoom("/", "gdisconnect");
52 // Wait for "connect" message to notify connection to the room,
53 // because if game loading is slow the message listener might
54 // not be ready too early.
57 const curPage
= clients
[sid
].page
;
58 notifyRoom(curPage
, "connect"); //Hall or Game
59 if (curPage
.indexOf("/game/") >= 0)
60 notifyRoom("/", "gconnect"); //notify main hall
65 const curPage
= clients
[sid
].page
;
66 socket
.send(JSON
.stringify({code:"pollclients",
67 sockIds: Object
.keys(clients
).filter(k
=>
68 k
!= sid
&& clients
[k
].page
== curPage
73 socket
.send(JSON
.stringify({code:"pollgamers",
74 sockIds: Object
.keys(clients
).filter(k
=>
75 k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0
79 // page change clients[sid].page --> obj.page
80 // TODO: some offline rooms don't need to receive disconnect event
81 notifyRoom(clients
[sid
].page
, "disconnect");
82 if (clients
[sid
].page
.indexOf("/game/") >= 0)
83 notifyRoom("/", "gdisconnect");
84 clients
[sid
].page
= obj
.page
;
85 // No need to notify connection: it's self-sent in .vue file
86 //notifyRoom(obj.page, "connect");
87 if (obj
.page
.indexOf("/game/") >= 0)
88 notifyRoom("/", "gconnect");
91 clients
[obj
.target
].sock
.send(JSON
.stringify(
92 {code:"askidentity",from:sid
}));
95 clients
[obj
.target
].sock
.send(JSON
.stringify(
96 {code:"asklastate",from:sid
}));
99 clients
[obj
.target
].sock
.send(JSON
.stringify(
100 {code:"askchallenge",from:sid
}));
104 // Check all clients playing, and send them a "askgame" message
105 let gameSids
= {}; //game ID --> [sid1, sid2]
106 const regexpGid
= /\/[a-zA-Z0-9]+$/;
107 Object
.keys(clients
).forEach(k
=> {
108 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
110 const gid
= clients
[k
].page
.match(regexpGid
)[0];
114 gameSids
[gid
].push(k
);
117 // Request only one client out of 2 (TODO: this is a bit heavy)
118 // Alt: ask game to all, and filter later?
119 Object
.keys(gameSids
).forEach(gid
=> {
120 const L
= gameSids
[gid
].length
;
122 ? Math
.floor(Math
.random() * Math
.floor(L
))
124 const rid
= gameSids
[gid
][idx
];
125 clients
[rid
].sock
.send(JSON
.stringify(
126 {code:"askgame", from: sid
}));
131 clients
[obj
.target
].sock
.send(JSON
.stringify(
132 {code:"askgame", from:sid
}));
135 clients
[obj
.target
].sock
.send(JSON
.stringify(
136 {code:"askfullgame", from:sid
}));
139 clients
[obj
.target
].sock
.send(JSON
.stringify(
140 {code:"fullgame", game:obj
.game
}));
143 clients
[obj
.target
].sock
.send(JSON
.stringify(
144 {code:"identity",user:obj
.user
}));
146 case "refusechallenge":
147 clients
[obj
.target
].sock
.send(JSON
.stringify(
148 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
150 case "deletechallenge":
151 clients
[obj
.target
].sock
.send(JSON
.stringify(
152 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
155 clients
[obj
.target
].sock
.send(JSON
.stringify(
156 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
159 clients
[obj
.target
].sock
.send(JSON
.stringify(
160 {code:"challenge", chall:obj
.chall
, from:sid
}));
165 clients
[obj
.target
].sock
.send(JSON
.stringify(
166 {code:"game", game:obj
.game
, from:sid
}));
170 // Notify all room except opponent and me:
171 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
175 notifyRoom(clients
[sid
].page
, "newchat", {chat:obj
.chat
});
177 // TODO: WebRTC instead in this case (most demanding?)
178 // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
180 clients
[obj
.target
].sock
.send(JSON
.stringify(
181 {code:"newmove", move:obj
.move}));
184 clients
[obj
.target
].sock
.send(JSON
.stringify(
185 {code:"lastate", state:obj
.state
}));
188 clients
[obj
.target
].sock
.send(JSON
.stringify(
189 {code:"resign", side:obj
.side
}));
192 clients
[obj
.target
].sock
.send(JSON
.stringify(
196 clients
[obj
.target
].sock
.send(JSON
.stringify(
197 {code:"drawoffer"}));
200 clients
[obj
.target
].sock
.send(JSON
.stringify(
201 {code:"draw", message:obj
.message
}));
205 const closeListener
= () => {
206 const page
= clients
[sid
].page
;
208 notifyRoom(page
, "disconnect");
209 if (page
.indexOf("/game/") >= 0)
210 notifyRoom("/", "gdisconnect"); //notify main hall
214 // Turn off old sock through current client:
215 clients
[sid
].sock
.send(JSON
.stringify({code:"duplicate"}));
217 // Potentially replace current connection:
218 clients
[sid
] = {sock: socket
, page: query
["page"]};
219 socket
.on("message", messageListener
);
220 socket
.on("close", closeListener
);