Skip to content

Commit

Permalink
WIP chat-server v2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
June1010 committed Dec 13, 2022
1 parent 74a8c2c commit 294e85a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion chat-server/src/queue-manager/manager.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CACHE_MANAGER, Inject } from '@nestjs/common';
import { CACHE_MANAGER, Inject, Injectable, Scope } from '@nestjs/common';
import { Cache } from 'cache-manager';
import * as Bull from 'bull';

@Injectable({ scope: Scope.DEFAULT })
export class ManagerService {
constructor(
@Inject(CACHE_MANAGER) private redisCache: Cache,
Expand All @@ -11,6 +12,7 @@ export class ManagerService {
const queue = new Bull(name);
queue.pause();
this.map.set(name, queue); // ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ์— key: "7:June1010", val: Queue Instance ์ €์žฅํ•ด์ฃผ๊ธฐ
console.log('generateQueue: ', this.map);
// queue.process(๋ฉ”์‹œ์ง€ ์ „์†กํ•˜๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜) [2] : process ๋“ฑ๋ก
// queue.pause() [1] : ๋ชจ์ง‘์‹ ์ฒญ/์ฐธ๊ฐ€์‹ ์ฒญ
// queue.resume() [3] : ์˜จ๋ผ์ธ์ผ๊ฒฝ์šฐ process ์žฌ๊ฐœ socket.onconnect()
Expand Down Expand Up @@ -46,10 +48,12 @@ export class ManagerService {

// ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ์—์„œ, name(key) ๊ฐ’์œผ๋กœ Queue Instance ๊ฐ€์ ธ์™€์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ธฐ
getQueue(name: string): Bull.Queue {
console.log('getQueue: ', this.map);
return this.map.get(name);
}

getQueueList(recruitId: string) {
console.log('getQueueList: ', this.map);
const keys = Array.from(this.map.keys()).filter(
(key) => key.split(':')[0] === recruitId,
);
Expand Down
4 changes: 4 additions & 0 deletions chat-server/src/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class SocketGateway implements OnGatewayDisconnect {
): Promise<void> {
// ํ•ด๋‹น ๋ชจ์ง‘์— ์žˆ๋Š” ๋ชจ๋“  ์‚ฌ์šฉ์ž์˜ ํ์— ๋„ฃ์–ด์ฃผ๊ธฐ
const { content } = data;
console.log('client_sent', data);
const { userId, recruitId } = await this.socketService.getCacheData(
socket.id,
);
Expand All @@ -63,6 +64,7 @@ export class SocketGateway implements OnGatewayDisconnect {
chat.recruitId = recruitId;
chat.content = content;
chat.createdAt = new Date();
console.log('client_sent:queueList', queueList);
const addWork = [];
queueList.map((queue: Bull.Queue) => {
addWork.push(queue.add(chat));
Expand All @@ -72,10 +74,12 @@ export class SocketGateway implements OnGatewayDisconnect {
}

async handleDisconnect(@ConnectedSocket() socket: Socket): Promise<void> {
console.log('๋Š๊น€');
const { recruitId, userId } = await this.socketService.getCacheData(
socket.id,
);
const queue = this.queueService.getQueue(`${recruitId}:${userId}`);
if (!queue) return;
queue.pause();
await this.socketService.delCacheData(socket.id);
}
Expand Down
1 change: 1 addition & 0 deletions chat-server/src/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class SocketService {
}

async saveRecentMessage(chatEntity: Chat) {
console.log('saveRecentMessage', chatEntity);
return this.chatModel.create(chatEntity);
}
}

0 comments on commit 294e85a

Please sign in to comment.