df44520d4d29115b39e610b92b77e0398e62e826
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 const sid
= query
["sid"];
21 const notifyRoom
= (page
,code
,obj
={},excluded
=[]) => {
22 Object
.keys(clients
).forEach(k
=> {
25 if (k
!= sid
&& clients
[k
].page
== page
)
27 clients
[k
].sock
.send(JSON
.stringify(Object
.assign(
28 {code:code
, from:sid
}, obj
)));
32 const messageListener
= (objtxt
) => {
33 let obj
= JSON
.parse(objtxt
);
34 if (!!obj
.target
&& !clients
[obj
.target
])
35 return; //receiver not connected, nothing we can do
39 // Turn off message listening, and send disconnect if needed:
40 socket
.removeListener("message", messageListener
);
41 socket
.removeListener("close", closeListener
);
42 if (clients
[sid
].page
!= obj
.page
)
44 notifyRoom(clients
[sid
].page
, "disconnect");
45 if (clients
[sid
].page
.indexOf("/game/") >= 0)
46 notifyRoom("/", "gdisconnect");
49 // Wait for "connect" message to notify connection to the room,
50 // because if game loading is slow the message listener might
51 // not be ready too early.
54 const curPage
= clients
[sid
].page
;
55 notifyRoom(curPage
, "connect"); //Hall or Game
56 if (curPage
.indexOf("/game/") >= 0)
57 notifyRoom("/", "gconnect"); //notify main hall
62 const curPage
= clients
[sid
].page
;
63 socket
.send(JSON
.stringify({code:"pollclients",
64 sockIds: Object
.keys(clients
).filter(k
=>
65 k
!= sid
&& clients
[k
].page
== curPage
70 socket
.send(JSON
.stringify({code:"pollgamers",
71 sockIds: Object
.keys(clients
).filter(k
=>
72 k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0
76 // page change clients[sid].page --> obj.page
77 // TODO: some offline rooms don't need to receive disconnect event
78 notifyRoom(clients
[sid
].page
, "disconnect");
79 if (clients
[sid
].page
.indexOf("/game/") >= 0)
80 notifyRoom("/", "gdisconnect");
81 clients
[sid
].page
= obj
.page
;
82 // No need to notify connection: it's self-sent in .vue file
83 //notifyRoom(obj.page, "connect");
84 if (obj
.page
.indexOf("/game/") >= 0)
85 notifyRoom("/", "gconnect");
88 clients
[obj
.target
].sock
.send(JSON
.stringify(
89 {code:"askidentity",from:sid
}));
92 clients
[obj
.target
].sock
.send(JSON
.stringify(
93 {code:"asklastate",from:sid
}));
96 clients
[obj
.target
].sock
.send(JSON
.stringify(
97 {code:"askchallenge",from:sid
}));
101 // Check all clients playing, and send them a "askgame" message
102 let gameSids
= {}; //game ID --> [sid1, sid2]
103 const regexpGid
= /\/[a-zA-Z0-9]+$/;
104 Object
.keys(clients
).forEach(k
=> {
105 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
107 const gid
= clients
[k
].page
.match(regexpGid
)[0];
111 gameSids
[gid
].push(k
);
114 // Request only one client out of 2 (TODO: this is a bit heavy)
115 // Alt: ask game to all, and filter later?
116 Object
.keys(gameSids
).forEach(gid
=> {
117 const L
= gameSids
[gid
].length
;
119 ? Math
.floor(Math
.random() * Math
.floor(L
))
121 const rid
= gameSids
[gid
][idx
];
122 clients
[rid
].sock
.send(JSON
.stringify(
123 {code:"askgame", from: sid
}));
128 clients
[obj
.target
].sock
.send(JSON
.stringify(
129 {code:"askgame", from:sid
}));
132 clients
[obj
.target
].sock
.send(JSON
.stringify(
133 {code:"askfullgame", from:sid
}));
136 clients
[obj
.target
].sock
.send(JSON
.stringify(
137 {code:"fullgame", game:obj
.game
}));
140 clients
[obj
.target
].sock
.send(JSON
.stringify(
141 {code:"identity",user:obj
.user
}));
143 case "refusechallenge":
144 clients
[obj
.target
].sock
.send(JSON
.stringify(
145 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
147 case "deletechallenge":
148 clients
[obj
.target
].sock
.send(JSON
.stringify(
149 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
152 clients
[obj
.target
].sock
.send(JSON
.stringify(
153 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
156 clients
[obj
.target
].sock
.send(JSON
.stringify(
157 {code:"challenge", chall:obj
.chall
, from:sid
}));
162 clients
[obj
.target
].sock
.send(JSON
.stringify(
163 {code:"game", game:obj
.game
, from:sid
}));
167 // Notify all room except opponent and me:
168 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
172 notifyRoom(clients
[sid
].page
, "newchat", {chat:obj
.chat
});
174 // TODO: WebRTC instead in this case (most demanding?)
175 // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
177 clients
[obj
.target
].sock
.send(JSON
.stringify(
178 {code:"newmove", move:obj
.move}));
181 clients
[obj
.target
].sock
.send(JSON
.stringify(
182 {code:"lastate", state:obj
.state
}));
185 clients
[obj
.target
].sock
.send(JSON
.stringify(
186 {code:"resign", side:obj
.side
}));
189 clients
[obj
.target
].sock
.send(JSON
.stringify(
193 clients
[obj
.target
].sock
.send(JSON
.stringify(
194 {code:"drawoffer"}));
197 clients
[obj
.target
].sock
.send(JSON
.stringify(
198 {code:"draw", message:obj
.message
}));
202 const closeListener
= () => {
203 const page
= clients
[sid
].page
;
205 notifyRoom(page
, "disconnect");
206 if (page
.indexOf("/game/") >= 0)
207 notifyRoom("/", "gdisconnect"); //notify main hall
211 // Turn off old sock through current client:
212 clients
[sid
].sock
.send(JSON
.stringify({code:"duplicate"}));
214 // Potentially replace current connection:
215 clients
[sid
] = {sock: socket
, page: query
["page"]};
216 socket
.on("message", messageListener
);
217 socket
.on("close", closeListener
);