Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] 공용 메시지 브로커 구현 및 적용 #16

Open
wants to merge 26 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cb9e499
feat: 메시지큐 구현
codemario318 Jan 21, 2025
316d744
feat: message-queue 모듈 등록
codemario318 Jan 22, 2025
5128b99
fix: 메시지큐 구현 제거
codemario318 Jan 22, 2025
8d058b4
feat: pub-sub 기능 구현
codemario318 Jan 22, 2025
dab1606
refactor: pub-sub 적용
codemario318 Jan 22, 2025
22284cc
fix: pub-sub 인터페이스 수정
codemario318 Jan 22, 2025
bb3efe4
feat: 메시지 브로커 수정
codemario318 Jan 22, 2025
69d6b1c
fix: 브로커 초기화 오류 수정
codemario318 Jan 22, 2025
36385ac
feat: 메시지 브로커 구현체 추가
codemario318 Jan 22, 2025
95262f6
feat: ReactiveMessageBroker 적용
codemario318 Jan 22, 2025
e2b760d
fix: 채팅 핸들러 수정
codemario318 Jan 22, 2025
01d3048
refactor: play 영역 pub-sub 모듈 적용
codemario318 Jan 22, 2025
aab69ef
refactor: pub-sub 이름 변경
codemario318 Jan 23, 2025
fe899b2
refactor: Broker 구조 변경
codemario318 Jan 23, 2025
6ff1c2d
refactor: broker 변경 사항 반영
codemario318 Jan 23, 2025
d7306a0
refactor: chat 서비스 broker 리펙토링 반영
codemario318 Jan 23, 2025
1d1971c
refactor: broker 모듈 위치 변경
codemario318 Jan 23, 2025
75bf70c
refactor: 공용 broker 수정
codemario318 Jan 23, 2025
3244f1a
chore: shared 의존성 추가
codemario318 Jan 23, 2025
5153f07
refactor: broker 모듈 변경 반영
codemario318 Jan 23, 2025
b09a1f7
refactor: reactive-message-broker 수정
codemario318 Jan 23, 2025
68df88e
chore: shared 패키지 의존성 추가
codemario318 Jan 23, 2025
f07a735
refactor: play clients 의존성 제거
codemario318 Jan 23, 2025
6856587
fix: play gateway 기능 복구
codemario318 Jan 23, 2025
3dcca09
refactor: broker 인터페이스 변경
codemario318 Jan 24, 2025
4d609af
refactor: broker 인터페이스 변경
codemario318 Jan 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/backend/src/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
import { ChatController } from './chat.controller';
import { ChatRepositoryMemory } from './repository/chat.memory.repository';
import { ChatService } from './chat.service';
import { ReactiveMessageBroker } from '@web08-booquiz/shared';

@Module({
controllers: [ChatController],
Expand All @@ -15,6 +16,10 @@ import { ChatService } from './chat.service';
provide: 'ChatRepository',
useClass: ChatRepositoryMemory,
},
{
provide: 'Broker',
useClass: ReactiveMessageBroker,
}
],
exports: [ChatService],
})
Expand Down
61 changes: 49 additions & 12 deletions apps/backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Injectable, Inject, NotFoundException } from '@nestjs/common';
import { ChatRepositoryMemory } from './repository/chat.memory.repository';
import {ChatMessage} from "@web08-booquiz/shared";
import { Broker, ChatMessage, MessageWithTopic, Player } from '@web08-booquiz/shared';

@Injectable()
export class ChatService {
constructor(
@Inject('ChatRepository')
private readonly chatRepository: ChatRepositoryMemory,
) {}
@Inject('Broker')
private readonly broker: Broker<MessageWithTopic<'chat' | 'leave', ChatMessage>>
) {
}

async set(id: string) {
this.chatRepository.set(id);
await this.chatRepository.set(id);
await this.broker.addPublisher(id);
}

async get(id: string) {
Expand All @@ -20,18 +24,51 @@ export class ChatService {
return this.chatRepository.get(id);
}

async add(id: string, chatMessage: ChatMessage) {
if (!(await this.chatRepository.has(id))) {
throw new NotFoundException('퀴즈 존에 대한 채팅이 존재하지 않습니다.');
}
return this.chatRepository.add(id, chatMessage);
async delete(id: string) {
await this.chatRepository.delete(id);
await this.broker.removePublisher(id);
}

async has(id: string) {
return await this.chatRepository.has(id);
async join(chatId: string, player: Player, handleSendMessage: (data: ChatMessage) => void) {
const unsubscribe = await this.broker.subscribe(chatId, async (message) => {
const { topic, data } = message;
const { clientId } = data;

switch (topic) {
case 'chat':
handleSendMessage(data);
return this.add(chatId, data);
case 'leave':
if (clientId === player.id) {
return unsubscribe();
}
}
});
}

async delete(id: string) {
return this.chatRepository.delete(id);
async send(chatId: string, chatMessage: ChatMessage) {
await this.broker.publish(chatId, {
topic: 'chat',
data: chatMessage,
});
}

async leave(chatId: string, clientId: string) {
await this.broker.publish(chatId, {
topic: 'leave',
data: {
clientId,
nickname: '',
message: '',
},
})
}

private async add(id: string, chatMessage: ChatMessage) {
if (!(await this.chatRepository.has(id))) {
throw new NotFoundException('퀴즈 존에 대한 채팅이 존재하지 않습니다.');
}

await this.chatRepository.add(id, chatMessage);
}
}
6 changes: 5 additions & 1 deletion apps/backend/src/core/SessionWsAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { RequestHandler } from 'express';
import { NestApplication } from '@nestjs/core';
import { Session } from 'express-session';

export interface SessionWithQuizZone extends Session {
quizZoneId?: string;
}

export interface WebSocketWithSession extends WebSocket {
session: Session;
session: SessionWithQuizZone;
}

export class SessionWsAdapter extends WsAdapter {
Expand Down
10 changes: 0 additions & 10 deletions apps/backend/src/play/entities/send-event.entity.ts

This file was deleted.

Loading