Skip to content

Commit

Permalink
Merge pull request #307 from boostcampwm-2024/feat/websocket-chatting
Browse files Browse the repository at this point in the history
🐛 fix: 채팅 날짜 변경 알림 로직 수정
  • Loading branch information
asn6878 authored Dec 29, 2024
2 parents 8271cb2 + 863e007 commit 1bde9a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion client/src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const formatDate = (dateString: string | undefined | null) => {
if (isNaN(date.getTime())) {
return "-";
}
return date.toISOString().split("T")[0];
return date.toLocaleDateString('en-CA');
} catch (error) {
console.error("Date formatting error:", error);
return "-";
Expand Down
24 changes: 10 additions & 14 deletions server/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Server, Socket } from 'socket.io';
import { RedisService } from '../common/redis/redis.service';
import { Injectable } from '@nestjs/common';
import { getRandomNickname } from '@woowa-babble/random-nickname';
import * as cron from 'node-cron';
import { Cron, CronExpression } from '@nestjs/schedule';

const CLIENT_KEY_PREFIX = 'socket_client:';
const CHAT_HISTORY_KEY = 'chat:history';
Expand All @@ -33,22 +33,13 @@ type BroadcastPayload = {
export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer()
server: Server;
private cronTask: cron.ScheduledTask;
private dayInit: boolean;

constructor(private readonly redisService: RedisService) {}

onModuleInit() {
this.startMidnightCron();
}

onModuleDestroy() {
this.cronTask.stop();
}

private startMidnightCron() {
this.cronTask = cron.schedule('0 0 * * *', () => {
this.emitMidnightMessage();
});
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
private midnightInitializer() {
this.dayInit = true;
}

private async emitMidnightMessage() {
Expand Down Expand Up @@ -110,6 +101,11 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {

await this.saveMessageToRedis(broadcastPayload);

if (this.dayInit) {
this.dayInit = false;
this.emitMidnightMessage();
}

this.server.emit('message', broadcastPayload);
}

Expand Down

0 comments on commit 1bde9a0

Please sign in to comment.