myheats

Live heats, scoring and leaderboard for sport events
git clone https://git.in0rdr.ch/myheats.git
Log | Files | Refs | Pull requests | README | LICENSE

commit ee6104a0c85442b96e095f9cb62b22dd01c3203f
parent ddb856ee2efeb60577a85cde2e6640d3db2f0312
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date:   Sat, 28 Sep 2024 12:08:33 +0200

fix(api): maintain clients in db moodule

Maintain the list of ws client connections only once in the db module.

Diffstat:
Msrc/api/db.cjs | 27++++++++++++++++-----------
Msrc/api/server.cjs | 10+++-------
2 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/src/api/db.cjs b/src/api/db.cjs @@ -1,7 +1,10 @@ const postgres = require('postgres'); -const watchers = new Set(); let scoreSubscription = null +// Keep track of connected websocket clients +// https://javascript.info/websocket +const clients = new Set(); + require('dotenv').config({ // Configure common config files and modes // - https://www.npmjs.com/package/dotenv @@ -341,17 +344,18 @@ async function setScore(heat, athlete, judge, score) { } } -async function removeWatcher(sock) { - console.log("- Watcher removed") - watchers.delete(sock) +function removeClient(sock) { + console.log("- Client removed") + clients.delete(sock) +} + +function addClient(sock) { + console.log("+ Client added") + clients.add(sock) } // add client to subscription async function watchScores(sock) { - // add client to watchlist - console.log("+ Watcher added") - watchers.add(sock); - if (scoreSubscription !== null) { // we are already subscribed to this publication return scoreSubscription @@ -363,7 +367,7 @@ async function watchScores(sock) { '*:scores', (row, { command, relation, key, old }) => { // distributed score updates to all connected clients - for(let c of watchers) { + for(let c of clients) { c.send(JSON.stringify({ "message": "Received new scores", "data": row, @@ -372,7 +376,7 @@ async function watchScores(sock) { }, () => { // callback on initial connect and potential reconnects - console.log("~ Watching scores") + console.log("~ Start watching scores, created scoreSubscription") } ) return scoreSubscription @@ -400,7 +404,8 @@ module.exports = { getScore, setScore, watchScores, - removeWatcher, + removeClient, + addClient, addAthleteToHeat, addAthlete, removeAthlete, diff --git a/src/api/server.cjs b/src/api/server.cjs @@ -698,10 +698,6 @@ function serverError(res, err) { // https://github.com/websockets/ws?tab=readme-ov-file#multiple-servers-sharing-a-single-https-server const wss1 = new ws.WebSocketServer({ noServer: true}); -// Keep track of connected websocket clients -// https://javascript.info/websocket -const clients = new Set(); - // Listen for websocket connections wss1.on('connection', function connection(sock) { sock.on('message', async function message(m) { @@ -710,6 +706,7 @@ wss1.on('connection', function connection(sock) { console.log(' Uncle roger hears:', msg); if (msg.method === 'watchScores') { + console.log(`~ Client requesting new scores`); db.watchScores(sock) } } catch (error) { @@ -723,12 +720,11 @@ wss1.on('connection', function connection(sock) { sock.on('close', function(event) { console.log(" Close event:", event.code); console.log(" Close reason:", event.reason); - clients.delete(sock); - db.removeWatcher(sock); + db.removeClient(sock); }); - clients.add(sock); console.log(`~ Received a new websocket connection`); + db.addClient(sock) }); // Listen to upgrade event