live action

This commit is contained in:
2025-11-26 19:58:46 +03:00
parent 732603559a
commit e07c5933ee
10 changed files with 349 additions and 159 deletions

View File

@@ -32,6 +32,29 @@ const io = new Server(server, {
}
});
let counter = 0;
let counterTimer: NodeJS.Timeout | null = null;
const broadcastCounter = () => {
io.emit("counter:update", { value: counter });
};
const startCounter = () => {
if (counterTimer) return;
counterTimer = setInterval(() => {
counter += 1;
broadcastCounter();
}, 1000);
};
const stopCounter = () => {
if (counterTimer) {
clearInterval(counterTimer);
counterTimer = null;
}
io.emit("counter:stopped", { value: counter });
};
io.use((socket, next) => {
const token = socket.handshake.auth?.token as string | undefined;
if (!token) {
@@ -51,6 +74,20 @@ io.on("connection", (socket) => {
socket.on("ping", () => {
socket.emit("pong", "pong");
});
socket.on("counter:start", (ack?: (payload: { running: boolean; value: number }) => void) => {
startCounter();
ack?.({ running: true, value: counter });
});
socket.on("counter:stop", (ack?: (payload: { running: boolean; value: number }) => void) => {
stopCounter();
ack?.({ running: false, value: counter });
});
socket.on("counter:status", (ack?: (payload: { running: boolean; value: number }) => void) => {
ack?.({ running: !!counterTimer, value: counter });
});
});
async function start() {