From a0b58ed0c05c1d45d4819fb11268ff7a2b4ee8c5 Mon Sep 17 00:00:00 2001 From: Ji Seungmin Date: Tue, 22 Aug 2023 15:04:31 +0900 Subject: [PATCH] =?UTF-8?q?[feat]:=20=EC=83=81=EB=8C=80=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=EA=B0=80=20=EC=B1=84=ED=8C=85=EB=B0=A9=EC=97=90=20?= =?UTF-8?q?=EC=9E=88=EB=8A=94=EC=A7=80=20=ED=99=95=EC=9D=B8=20=ED=9B=84=20?= =?UTF-8?q?=EC=95=8C=EB=A6=BC=20=EC=A0=84=EC=86=A1=20=EC=97=AC=EB=B6=80?= =?UTF-8?q?=EB=A5=BC=20=EA=B2=B0=EC=A0=95=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- socket.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/socket.js b/socket.js index df50bff..6fbe9fd 100644 --- a/socket.js +++ b/socket.js @@ -54,6 +54,7 @@ module.exports = (io) => { //사용자가 특정 채팅방에 참여하려고할때 클라이언트에서 발생함, 이때 roomId와 userId를 매개변수로 전달 socket.on('joinRoom', (roomId, userId) => { socket.join(roomId); //소켓을 roomId에 지정된 방에 조인 + socket.userId = userId; //사용자 ID를 소켓에 연결 console.log(`User ${userId} joined room ${roomId}`); }); @@ -96,7 +97,19 @@ module.exports = (io) => { // 메시지 저장 await newMessage.save(); //저장 메시지 생성 - const { otherFcm } = await getFcm(otherId); //fcm 토큰 정보 받아오기 + const socketsInRoom = await io.in(roomId).fetchSockets(); //모든 소켓의 회원 ID 목록을 가지고 옴 + const isInRoom = socketsInRoom.some(s => s.userId === otherId); // 해당 방에 otherId가 있는지 확인 + + //만약 방에 있다면, 알림 전송 + if (isInRoom) { + const { otherFcm } = await getFcm(otherId); //fcm 토큰 정보 받아오기 + + if (otherFcm) { + await sendNotificationToToken(otherFcm, userId, newMessage.content); + } else { + console.error('No valid FCM token available'); + } + } // 같은 채팅방에 있는 모든 클라이언트에게 메시지 전송 io.to(roomId).emit('message', { @@ -104,12 +117,6 @@ module.exports = (io) => { username: newMessage.username, time: newMessage.time, }); - - if (otherFcm) { - await sendNotificationToToken(otherFcm, userId, newMessage.content); - } else { - console.error('No valid FCM token available'); - } }); // 클라이언트가 방을 나갈 때 실행할 이벤트 핸들러