fixed bugs with the web socket

pull/12/head
DmitriyA 2025-04-02 19:49:13 -04:00
parent 49a5471c01
commit 23438a0e7f
1 changed files with 9 additions and 0 deletions

View File

@ -84,6 +84,15 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
} }
} }
@SubscribeMessage('subscribe-metric')
async handleSubscribeMetric(client: Socket, payload: { metric: string, interval?: number }) {
const stopUpdates = await this.sendPeriodicUpdates(payload.metric, payload.interval);
// Сохраняем функцию остановки для этого клиента
client.on('disconnect', () => stopUpdates());
client.on('unsubscribe-metric', () => stopUpdates());
}
// Метод для периодической отправки обновлений // Метод для периодической отправки обновлений
async sendPeriodicUpdates(metric: string, interval: number = 5000) { async sendPeriodicUpdates(metric: string, interval: number = 5000) {
const timer = setInterval(async () => { const timer = setInterval(async () => {