From 95653e0afeed0a4b637bf55ecf4bba5e820da094 Mon Sep 17 00:00:00 2001 From: TaeHoon0 Date: Mon, 22 Jul 2024 23:03:48 +0900 Subject: [PATCH] =?UTF-8?q?[#39]=20fix=20:=20socket=20=EC=97=B0=EA=B2=B0,?= =?UTF-8?q?=20=ED=95=B4=EC=A0=9C=20=EC=97=90=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/jwt/RefreshTokenRepository.java | 1 - .../CatchStudy/service/ChatService.java | 38 +++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/example/CatchStudy/global/jwt/RefreshTokenRepository.java b/src/main/java/com/example/CatchStudy/global/jwt/RefreshTokenRepository.java index 375194d..d5ae7df 100644 --- a/src/main/java/com/example/CatchStudy/global/jwt/RefreshTokenRepository.java +++ b/src/main/java/com/example/CatchStudy/global/jwt/RefreshTokenRepository.java @@ -20,7 +20,6 @@ public void save(String refreshToken, String accessToken) { } public String find(String accessToken) { - log.info(accessToken); return redisTemplate.opsForValue().get(KEY_PREFIX + accessToken); } diff --git a/src/main/java/com/example/CatchStudy/service/ChatService.java b/src/main/java/com/example/CatchStudy/service/ChatService.java index 4d3202c..9738c94 100644 --- a/src/main/java/com/example/CatchStudy/service/ChatService.java +++ b/src/main/java/com/example/CatchStudy/service/ChatService.java @@ -11,12 +11,18 @@ import com.example.CatchStudy.repository.*; import lombok.RequiredArgsConstructor; import org.springframework.context.event.EventListener; +import org.springframework.messaging.simp.SimpMessageHeaderAccessor; +import org.springframework.messaging.simp.stomp.StompHeaderAccessor; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.messaging.support.MessageHeaderAccessor; +import org.springframework.messaging.support.NativeMessageHeaderAccessor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.socket.messaging.SessionConnectEvent; import org.springframework.web.socket.messaging.SessionDisconnectEvent; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -33,7 +39,8 @@ public class ChatService { private final StudyCafeRepository studyCafeRepository; private final UsersService usersService; private final ChatNotificationRepository chatNotificationRepository; - private final Map> chatRoomMap = new ConcurrentHashMap<>(); // chatRoomId, 접속한 유저 ID들 + private final Map> chatRoomMap = new ConcurrentHashMap<>(); // chatRoomId, + private final Map sessionToChatRoom = new ConcurrentHashMap<>(); @Transactional public ChatRoomResponseDto createChatRoom(ChatRoomRequestDto chatRoomRequestDto) { @@ -105,28 +112,29 @@ public MessageResponseDto createMessage(long chatRoomId, MessageRequestDto messa @EventListener public void handleSessionConnect(SessionConnectEvent event) { - long chatRoomId = Long.parseLong((String) event.getMessage().getHeaders().get("chatRoomId")); + MessageHeaderAccessor accessor = NativeMessageHeaderAccessor.getAccessor(event.getMessage(), SimpMessageHeaderAccessor.class); + GenericMessage generic = (GenericMessage) accessor.getHeader("simpConnectMessage"); + Map nativeHeaders = (Map) generic.getHeaders().get("nativeHeaders"); + Long chatRoomId = Long.parseLong ((String) ((List) nativeHeaders.get("chatRoomId")).get(0)); + String sessionId = (String) generic.getHeaders().get("simpSessionId"); long userId = usersService.getCurrentUserId(); - List userList = chatRoomMap.get(chatRoomId); - if (userList == null) userList = new ArrayList<>(); - userList.add(userId); + Map userMap = chatRoomMap.get(chatRoomId); + userMap.put(sessionId, userId); - chatRoomMap.put(chatRoomId, userList); + chatRoomMap.put(chatRoomId, userMap); + sessionToChatRoom.put(sessionId, chatRoomId); } @EventListener public void handleSessionDisconnect(SessionDisconnectEvent event) { - long chatRoomId = Long.parseLong((String) event.getMessage().getHeaders().get("chatRoomId")); - long userId = usersService.getCurrentUserId(); - - List userList = chatRoomMap.get(chatRoomId); - if(userList == null) return; - userList.remove(userId); + StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage()); + String sessionId = headerAccessor.getSessionId(); + Long chatRoomId = sessionToChatRoom.get(sessionId); - if (userList.isEmpty()) chatRoomMap.remove(chatRoomId); - else chatRoomMap.put(chatRoomId, userList); + Map userMap = chatRoomMap.get(chatRoomId); + userMap.remove(sessionId); - chatRoomMap.put(chatRoomId, userList); + if (userMap.isEmpty()) chatRoomMap.remove(chatRoomId); } }