From 294e85afaaf8ea165bcb6af5e9a43893607ab277 Mon Sep 17 00:00:00 2001 From: June1010 Date: Tue, 13 Dec 2022 18:50:36 +0900 Subject: [PATCH] WIP chat-server v2.1.1 --- chat-server/src/queue-manager/manager.service.ts | 6 +++++- chat-server/src/socket.gateway.ts | 4 ++++ chat-server/src/socket.service.ts | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/chat-server/src/queue-manager/manager.service.ts b/chat-server/src/queue-manager/manager.service.ts index 16b1ed5..fb1f3e9 100644 --- a/chat-server/src/queue-manager/manager.service.ts +++ b/chat-server/src/queue-manager/manager.service.ts @@ -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, @@ -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() @@ -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, ); diff --git a/chat-server/src/socket.gateway.ts b/chat-server/src/socket.gateway.ts index 533c87b..2384903 100644 --- a/chat-server/src/socket.gateway.ts +++ b/chat-server/src/socket.gateway.ts @@ -54,6 +54,7 @@ export class SocketGateway implements OnGatewayDisconnect { ): Promise { // 해당 모집에 있는 모든 사용자의 큐에 넣어주기 const { content } = data; + console.log('client_sent', data); const { userId, recruitId } = await this.socketService.getCacheData( socket.id, ); @@ -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)); @@ -72,10 +74,12 @@ export class SocketGateway implements OnGatewayDisconnect { } async handleDisconnect(@ConnectedSocket() socket: Socket): Promise { + 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); } diff --git a/chat-server/src/socket.service.ts b/chat-server/src/socket.service.ts index 97bf04b..07a7db1 100644 --- a/chat-server/src/socket.service.ts +++ b/chat-server/src/socket.service.ts @@ -38,6 +38,7 @@ export class SocketService { } async saveRecentMessage(chatEntity: Chat) { + console.log('saveRecentMessage', chatEntity); return this.chatModel.create(chatEntity); } }