From: Carl Worth Date: Thu, 8 Dec 2022 06:28:19 +0000 (-0800) Subject: Add a new 'reset' event from server to client before replay X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=ecd4364ec599f5f14ff8825ac74bee717151fe4c;p=zombocom-ai Add a new 'reset' event from server to client before replay This handles the case of a client that persists across a server restart, (or various other disconnect/reconnect scenarios). Prior to this commit, the client could end up with multiple copies of images through the following sequence: 1. A client is chugging along with the server and has a set of images 2. The server is stopped and restarted 3. When the server comes up again it sees the client connect and so it replays all images Boom. The client now has multiple copies of any image it already had. To fix this, the server now sends a 'reset' event prior to sending any replay of images. When the client gets this, it clears out all images it has. --- diff --git a/index.html b/index.html index f5eaea0..ad53d86 100644 --- a/index.html +++ b/index.html @@ -131,6 +131,10 @@ mute.addEventListener("click", () => { comments.appendChild(item); }); + socket.on('reset', () => { + images.replaceChildren(); + }); + socket.on('image', (image) => { const figure = document.createElement('figure'); const img = document.createElement('img'); diff --git a/index.js b/index.js index cd1911d..9d4028f 100644 --- a/index.js +++ b/index.js @@ -78,6 +78,7 @@ io.on('connection', (socket) => { console.log("Connection from client with " + socket.request.session.views + " views."); // Replay old comments and images to a newly-joining client + socket.emit('reset'); state.comments.forEach((comment) => { socket.emit('comment', comment) });