Skip to content

Commit

Permalink
Adding basic send delay
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmunro committed Feb 2, 2024
1 parent 2dd24ad commit 79a412a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ process.env.TZ = 'Europe/London';
type WebSocketData = {
url: URL;
id: string;
ready: boolean;
};

const randomId = () => {
Expand All @@ -20,16 +21,22 @@ const app = bunExpress({
websocket: {
open(ws) {
ws.data.id = randomId();
ws.data.ready = false;

setTimeout(
() => {
ws.data.ready = true;
},
process.env.SEND_DELAY ? parseInt(process.env.SEND_DELAY) : 3000
);

if (ws.data.url.pathname == '/pub') {
pubSockets.push(ws);
console.log('new publisher', ws.data.id);
}

if (ws.data.url.pathname == '/sub') {
setTimeout(() => {
subSockets.push(ws);
}, 500);
subSockets.push(ws);

console.log('new subscriber', ws.data.id);
}
Expand Down Expand Up @@ -72,7 +79,7 @@ setInterval(() => {
// Echo data back to sub sockets
if (latestFrame == null) return;
for (const sub of subSockets) {
sub.send(latestFrame);
if (sub.data.ready) sub.send(latestFrame);
}
}, sendInterval);

Expand Down

0 comments on commit 79a412a

Please sign in to comment.