fixed bugs
This commit is contained in:
@@ -19,9 +19,9 @@ import { RedisModule } from './redis/redis.module';
|
|||||||
entities: [__dirname + '/**/*.entity{.ts,.js}'],
|
entities: [__dirname + '/**/*.entity{.ts,.js}'],
|
||||||
synchronize: process.env.DB_SYNC === 'true',
|
synchronize: process.env.DB_SYNC === 'true',
|
||||||
}),
|
}),
|
||||||
|
RedisModule,
|
||||||
SocketModule,
|
SocketModule,
|
||||||
NotificationModule,
|
NotificationModule,
|
||||||
RedisModule,
|
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||||
import { createClient, RedisClientType } from 'redis';
|
import { createClient, RedisClientType } from 'redis';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RedisService implements OnModuleInit {
|
export class RedisService implements OnModuleInit {
|
||||||
|
private readonly logger = new Logger(RedisService.name);
|
||||||
private client: RedisClientType;
|
private client: RedisClientType;
|
||||||
|
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
@@ -10,10 +11,22 @@ export class RedisService implements OnModuleInit {
|
|||||||
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
|
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('error', (err) => console.error('Redis Error:', err));
|
this.client.on('ready', () => this.logger.log('Redis is ready!'));
|
||||||
|
this.client.on('error', (err) =>
|
||||||
|
this.logger.error('Redis Error:', err.message || err),
|
||||||
|
);
|
||||||
await this.client.connect();
|
await this.client.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async waitForReady(retries = 10, interval = 200): Promise<boolean> {
|
||||||
|
for (let i = 0; i < retries; i++) {
|
||||||
|
if (this.client?.isOpen) return true;
|
||||||
|
await new Promise((res) => setTimeout(res, interval));
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
getClient() {
|
getClient() {
|
||||||
return this.client;
|
return this.client;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ export class SocketGateway implements OnGatewayConnection {
|
|||||||
|
|
||||||
constructor(private socketService: SocketService) {}
|
constructor(private socketService: SocketService) {}
|
||||||
|
|
||||||
|
async afterInit() {
|
||||||
|
await this.socketService.clearAllOnlineClients();
|
||||||
|
}
|
||||||
|
|
||||||
async handleConnection(client: Socket) {
|
async handleConnection(client: Socket) {
|
||||||
const token: ClientToken = client.handshake.auth.token;
|
const token: ClientToken = client.handshake.auth.token;
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,25 @@ export class SocketService {
|
|||||||
private readonly redisService: RedisService,
|
private readonly redisService: RedisService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
async clearAllOnlineClients() {
|
||||||
|
const ready = await this.redisService.waitForReady();
|
||||||
|
|
||||||
|
if (!ready) {
|
||||||
|
this.logger.warn('Redis is not ready after retries. Skipping cleanup.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = this.redisService.getClient();
|
||||||
|
const keys = await client.keys('online_clients:*');
|
||||||
|
for (const key of keys) {
|
||||||
|
await client.del(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`🧹 Cleared ${keys.length} online client records from Redis.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private getRedisKey(token: ClientToken): string {
|
private getRedisKey(token: ClientToken): string {
|
||||||
return `online_clients:${token.user_id}-${token.telephone_id}`;
|
return `online_clients:${token.user_id}-${token.telephone_id}`;
|
||||||
}
|
}
|
||||||
@@ -87,10 +106,6 @@ export class SocketService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getClientKey(token: ClientToken): string {
|
|
||||||
return `${token.user_id}-${token.telephone_id}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async addOnlineClient(token: any, socketId: string) {
|
async addOnlineClient(token: any, socketId: string) {
|
||||||
const client = this.redisService.getClient();
|
const client = this.redisService.getClient();
|
||||||
const key = this.getRedisKey(token);
|
const key = this.getRedisKey(token);
|
||||||
|
|||||||
Reference in New Issue
Block a user