-
Notifications
You must be signed in to change notification settings - Fork 1
WebSocket 세션 처리 #102
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
base: develop
Are you sure you want to change the base?
WebSocket 세션 처리 #102
Changes from all commits
fc5526f
c964ab1
207278f
1942ecd
7822e4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.market.saessag.domain.chat.dto; | ||
|
|
||
| import com.market.saessag.global.response.SuccessCode; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class ChatRoomCreateResponse { | ||
| private SuccessCode successCode; | ||
| private ChatRoomResponse chatRoomResponse; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package com.market.saessag.domain.chat.service; | ||
|
|
||
| import com.market.saessag.domain.chat.dto.ChatRoomCreateResponse; | ||
| import com.market.saessag.domain.chat.dto.ChatRoomResponse; | ||
| import com.market.saessag.domain.chat.entity.ChatMessage; | ||
| import com.market.saessag.domain.chat.entity.ChatRoom; | ||
|
|
@@ -14,14 +15,17 @@ | |
| import com.market.saessag.domain.user.repository.UserRepository; | ||
| import com.market.saessag.global.exception.CustomException; | ||
| import com.market.saessag.global.exception.ErrorCode; | ||
| import com.market.saessag.global.response.SuccessCode; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpSession; | ||
| import jakarta.transaction.Transactional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.util.Pair; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
|
|
@@ -34,7 +38,7 @@ public class ChatRoomService { | |
| private final ChatSubscriptionRepository chatSubscriptionRepository; | ||
|
|
||
| //방 생성 (방 존재 시 채팅방 반환) | ||
| public ChatRoomResponse createOrGetChatRoom(Long productId, Long buyerId, Long sellerId) { | ||
| public ChatRoomCreateResponse createOrGetChatRoom(Long productId, Long buyerId, Long sellerId) { | ||
| Product product = productRepository.findById(productId) | ||
| .orElseThrow(() -> new CustomException(ErrorCode.PRODUCT_NOT_FOUND)); | ||
|
|
||
|
|
@@ -44,21 +48,34 @@ public ChatRoomResponse createOrGetChatRoom(Long productId, Long buyerId, Long s | |
| User seller = userRepository.findById(sellerId) | ||
| .orElseThrow(() -> new IllegalArgumentException("판매자를 찾을 수 없습니다.")); | ||
|
|
||
| ChatRoom chatRoom = chatRoomRepository.findByProductAndBuyerAndSeller(product, buyer, seller) | ||
| .orElseGet(() -> { | ||
| ChatRoom newRoom = ChatRoom.builder() | ||
| .product(product) | ||
| .buyer(buyer) | ||
| .seller(seller) | ||
| .build(); | ||
| Optional<ChatRoom> chatRoom = chatRoomRepository.findByProductAndBuyerAndSeller(product, buyer, seller); | ||
|
|
||
| return chatRoomRepository.save(newRoom); | ||
| }); | ||
| // 새로운 채팅방 생성하는 경우 | ||
| if (chatRoom.isEmpty()) { | ||
| ChatRoom newRoom = ChatRoom.builder() | ||
| .product(product) | ||
| .buyer(buyer) | ||
| .seller(seller) | ||
| .build(); | ||
|
|
||
| chatRoomRepository.save(newRoom); | ||
|
|
||
| // 구독 목록에 추가 | ||
| subscribeUserToChatRoom(buyer, newRoom); | ||
| subscribeUserToChatRoom(seller, newRoom); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 새로운 Room을 생성하고, 판매자와 구매자를 추가한다.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 방을 생성하는 로직이 이 부분 말고 쓰이는 곳이 없어서 따로 분리하여 관리는 안 했는데, createNewRoom() 같은 메서드로 분리하면 해당 로직을 읽기 편해질 것 같다고 생각합니다! |
||
|
|
||
| return ChatRoomCreateResponse.builder() | ||
| .successCode(SuccessCode.ROOM_CREATED) | ||
| .chatRoomResponse(chatRoomResponseEntity(newRoom)) | ||
| .build(); | ||
| } | ||
|
|
||
| subscribeUserToChatRoom(buyer, chatRoom); | ||
| subscribeUserToChatRoom(seller, chatRoom); | ||
| // 기존 채팅방 불러오는 경우 | ||
|
|
||
| return chatRoomResponseEntity(chatRoom); | ||
| return ChatRoomCreateResponse.builder() | ||
| .successCode(SuccessCode.ROOM_FETCHED) | ||
| .chatRoomResponse(chatRoomResponseEntity(chatRoom.get())) | ||
| .build(); | ||
| } | ||
|
|
||
| // 유저의 모든 채팅방 반환 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.market.saessag.global.interceptor; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpSession; | ||
| import org.springframework.http.server.ServerHttpRequest; | ||
| import org.springframework.http.server.ServerHttpResponse; | ||
| import org.springframework.http.server.ServletServerHttpRequest; | ||
| import org.springframework.web.socket.WebSocketHandler; | ||
| import org.springframework.web.socket.server.HandshakeInterceptor; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class WebSocketHandShakeInterceptor implements HandshakeInterceptor { | ||
|
|
||
| @Override | ||
| public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { | ||
| if (request instanceof ServletServerHttpRequest) { | ||
| ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; | ||
| HttpSession session = servletRequest.getServletRequest().getSession(false); | ||
|
|
||
| if (session != null) { | ||
| // 세션 정보 유저 정보에서 가져와 WebSocket 세션에 저장 | ||
| attributes.put("userProfile", session.getAttribute("userProfile")); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { | ||
|
|
||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
엔드포인트를 지정하지 않은 이유가 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/api/chat/room 엔드포인트에 POST 요청을 보내면 방 생성 엔드포인트로 괜찮을거라 생각했었는데 다시 보니 /create 등으로 지정하는게 더 좋을 것 같습니다!