server for my chess game
| -rw-r--r-- | src/index.js | 91 |
1 files changed, 45 insertions, 46 deletions
diff --git a/src/index.js b/src/index.js index 49dd173..98ae2ba 100644 --- a/src/index.js +++ b/src/index.js @@ -7,36 +7,44 @@ const PORT = process.env.PORT || 3000; const wss = new WebSocket.Server({ port: PORT }); const HEADERS = { - move: "M", + relay: "R", joinrequest: "J", hostrequest: "H", stopgame: "K", ping: "P", - startgame: "S", - listgames: "L", - request: "R", + signal: "S", }; let games = {}; -function send_packet(string, header, client) { - const packet = gdCom.putVar({ data: string, header: header }); +function str_obj(obj) { + return JSON.stringify(obj, null, 0); +} + +function send_packet(data, header, client) { + const packet = gdCom.putVar({ data: data, header: header }); client.send(packet); } -console.log(`Server started on port ${PORT}`); -wss.on("connection", (ws) => { - console.log("client connected"); +function send_group_packet(data, header, clients) { + clients.forEach((client) => { + send_packet(data, header, client); + }); +} +console.log(`Server started on port ${PORT}`); +wss.on("connection", (ws, req) => { + console.log(`client connected (${req.socket.remoteAddress})`); // on message recieved ws.on("message", (message) => { let recieve = gdCom.getVar(Buffer.from(message)).value; let data = recieve.data; let header = recieve.header; + console.log(`recieved ${str_obj(data)}`); if (header) { switch (header) { - case HEADERS.move: - handle_move(data, ws); + case HEADERS.relay: + dual_relay(data, ws); break; case HEADERS.joinrequest: handle_joinrequest(data, ws); @@ -50,13 +58,8 @@ wss.on("connection", (ws) => { case HEADERS.ping: send_packet("", HEADERS.ping, ws); break; - case HEADERS.startgame: - games[data].forEach((client) => { - send_packet("", HEADERS.startgame, client); - }); - break; - case HEADERS.request: - handle_request(data, ws); + case HEADERS.signal: + signal_other(data, ws); break; default: console.log(`header ${header} unknown`); @@ -66,16 +69,6 @@ wss.on("connection", (ws) => { }); }); -function handle_move(data, ws) { - if (games[data.gamecode].includes(ws)) { - games[data.gamecode].forEach((client) => { - send_packet(data, HEADERS.move, client); - }); - } else { - console.log("game not found!"); - } -} - function handle_joinrequest(data, ws) { console.log("joinrequest", data); if (games[data] !== undefined) { @@ -106,27 +99,33 @@ function handle_hostrequest(data, ws) { } function handle_stop(data, ws) { - console.log("stopgame " + data); - if (games[data].includes(ws)) { - games[data].forEach((client) => { - send_packet("", HEADERS.stopgame, client); - }); - delete games[data]; - } else { - console.log("wtf dude"); + if (data.gamecode in games) { + if (games[data.gamecode].includes(ws)) { + console.log("stopgame " + data.gamecode); + send_group_packet(data.reason, HEADERS.stopgame, games[data.gamecode]); + delete games[data.gamecode]; + } } } -function handle_request(data, ws) { - let i = games[data.gamecode].indexOf(ws); - if (i !== -1) { - let sendto = games[data.gamecode][i ? 0 : 1]; - if (data.answering) { - send_packet(data.answer, data.type, sendto); - console.log(`sending request result ${data.answer}`); - } else { - console.log(`sending request ${data.question}`); - send_packet(data.question, data.type, sendto); +// relays to both clients +function dual_relay(data, ws) { + if (data.gamecode in games) { + if (games[data.gamecode].includes(ws)) { + send_group_packet(data, HEADERS.relay, games[data.gamecode]); + console.log(`relaying ${str_obj(data)} to both clients`); + } + } +} + +// relays to the other client +function signal_other(data, ws) { + if (data.gamecode in games) { + let i = games[data.gamecode].indexOf(ws); + if (i !== -1) { + let sendto = games[data.gamecode][i ? 0 : 1]; + send_packet(data, HEADERS.signal, sendto); + console.log(`sending signal ${str_obj(data)}`); } } } |