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 // From obj.page to clients[sid].page (TODO: unclear)
43 if (clients
[sid
].page
!= obj
.page
)
45 notifyRoom(obj
.page
, "disconnect");
46 if (obj
.page
.indexOf("/game/") >= 0)
47 notifyRoom("/", "gdisconnect");
50 // Wait for "connect" message to notify connection to the room,
51 // because if game loading is slow the message listener might
52 // not be ready too early.
55 const curPage
= clients
[sid
].page
;
56 notifyRoom(curPage
, "connect"); //Hall or Game
57 if (curPage
.indexOf("/game/") >= 0)
58 notifyRoom("/", "gconnect"); //notify main hall
63 const curPage
= clients
[sid
].page
;
64 socket
.send(JSON
.stringify({code:"pollclients",
65 sockIds: Object
.keys(clients
).filter(k
=>
66 k
!= sid
&& clients
[k
].page
== curPage
71 socket
.send(JSON
.stringify({code:"pollgamers",
72 sockIds: Object
.keys(clients
).filter(k
=>
73 k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0
77 // page change clients[sid].page --> obj.page
78 // TODO: some offline rooms don't need to receive disconnect event
79 notifyRoom(clients
[sid
].page
, "disconnect");
80 if (clients
[sid
].page
.indexOf("/game/") >= 0)
81 notifyRoom("/", "gdisconnect");
82 clients
[sid
].page
= obj
.page
;
83 // No need to notify connection: it's self-sent in .vue file
84 //notifyRoom(obj.page, "connect");
85 if (obj
.page
.indexOf("/game/") >= 0)
86 notifyRoom("/", "gconnect");
89 clients
[obj
.target
].sock
.send(JSON
.stringify(
90 {code:"askidentity",from:sid
}));
93 clients
[obj
.target
].sock
.send(JSON
.stringify(
94 {code:"asklastate",from:sid
}));
97 clients
[obj
.target
].sock
.send(JSON
.stringify(
98 {code:"askchallenge",from:sid
}));
102 // Check all clients playing, and send them a "askgame" message
103 let gameSids
= {}; //game ID --> [sid1, sid2]
104 const regexpGid
= /\/[a-zA-Z0-9]+$/;
105 Object
.keys(clients
).forEach(k
=> {
106 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
108 const gid
= clients
[k
].page
.match(regexpGid
)[0];
112 gameSids
[gid
].push(k
);
115 // Request only one client out of 2 (TODO: this is a bit heavy)
116 // Alt: ask game to all, and filter later?
117 Object
.keys(gameSids
).forEach(gid
=> {
118 const L
= gameSids
[gid
].length
;
120 ? Math
.floor(Math
.random() * Math
.floor(L
))
122 const rid
= gameSids
[gid
][idx
];
123 clients
[rid
].sock
.send(JSON
.stringify(
124 {code:"askgame", from: sid
}));
129 clients
[obj
.target
].sock
.send(JSON
.stringify(
130 {code:"askgame", from:sid
}));
133 clients
[obj
.target
].sock
.send(JSON
.stringify(
134 {code:"askfullgame", from:sid
}));
137 clients
[obj
.target
].sock
.send(JSON
.stringify(
138 {code:"fullgame", game:obj
.game
}));
141 clients
[obj
.target
].sock
.send(JSON
.stringify(
142 {code:"identity",user:obj
.user
}));
144 case "refusechallenge":
145 clients
[obj
.target
].sock
.send(JSON
.stringify(
146 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
148 case "deletechallenge":
149 clients
[obj
.target
].sock
.send(JSON
.stringify(
150 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
153 clients
[obj
.target
].sock
.send(JSON
.stringify(
154 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
157 clients
[obj
.target
].sock
.send(JSON
.stringify(
158 {code:"challenge", chall:obj
.chall
, from:sid
}));
163 clients
[obj
.target
].sock
.send(JSON
.stringify(
164 {code:"game", game:obj
.game
, from:sid
}));
168 // Notify all room except opponent and me:
169 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
173 notifyRoom(clients
[sid
].page
, "newchat", {chat:obj
.chat
});
175 // TODO: WebRTC instead in this case (most demanding?)
176 // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
178 clients
[obj
.target
].sock
.send(JSON
.stringify(
179 {code:"newmove", move:obj
.move}));
182 clients
[obj
.target
].sock
.send(JSON
.stringify(
183 {code:"lastate", state:obj
.state
}));
186 clients
[obj
.target
].sock
.send(JSON
.stringify(
187 {code:"resign", side:obj
.side
}));
190 clients
[obj
.target
].sock
.send(JSON
.stringify(
194 clients
[obj
.target
].sock
.send(JSON
.stringify(
195 {code:"drawoffer"}));
198 clients
[obj
.target
].sock
.send(JSON
.stringify(
199 {code:"draw", message:obj
.message
}));
203 const closeListener
= () => {
204 const page
= clients
[sid
].page
;
206 notifyRoom(page
, "disconnect");
207 if (page
.indexOf("/game/") >= 0)
208 notifyRoom("/", "gdisconnect"); //notify main hall
212 // Turn off old sock through current client:
213 clients
[sid
].sock
.send(JSON
.stringify({code:"duplicate"}));
215 // Potentially replace current connection:
216 clients
[sid
] = {sock: socket
, page: query
["page"]};
217 socket
.on("message", messageListener
);
218 socket
.on("close", closeListener
);