Skip to content

Commit 856819e

Browse files
authored
Merge pull request #247 from prgrms-web-devcourse-final-project/Fix/241
Fix: 프론트 요청사항에 따른 WebRTC 시그널링 서버 수정 (#241)
2 parents e8511df + 0bf1b25 commit 856819e

File tree

7 files changed

+90
-141
lines changed

7 files changed

+90
-141
lines changed

src/main/java/com/back/global/websocket/webrtc/controller/WebRTCSignalingController.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.back.global.websocket.dto.WebSocketSessionInfo;
77
import com.back.global.websocket.service.WebSocketSessionManager;
88
import com.back.global.websocket.util.WebSocketAuthHelper;
9+
import com.back.global.websocket.webrtc.dto.WebRTCErrorResponse;
910
import com.back.global.websocket.webrtc.dto.media.WebRTCMediaStateResponse;
1011
import com.back.global.websocket.webrtc.dto.media.WebRTCMediaToggleRequest;
1112
import com.back.global.websocket.webrtc.dto.signal.*;
@@ -16,7 +17,6 @@
1617
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
1718
import org.springframework.messaging.handler.annotation.MessageMapping;
1819
import org.springframework.messaging.handler.annotation.Payload;
19-
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
2020
import org.springframework.messaging.simp.SimpMessagingTemplate;
2121
import org.springframework.stereotype.Controller;
2222
import org.springframework.validation.annotation.Validated;
@@ -35,13 +35,10 @@ public class WebRTCSignalingController {
3535

3636
// WebRTC Offer 메시지 처리
3737
@MessageMapping("/webrtc/offer")
38-
public void handleOffer(@Validated @Payload WebRTCOfferRequest request,
39-
SimpMessageHeaderAccessor headerAccessor,
40-
Principal principal) {
38+
public void handleOffer(@Validated @Payload WebRTCOfferRequest request, Principal principal) {
4139
CustomUserDetails userDetails = WebSocketAuthHelper.extractUserDetails(principal);
4240
if (userDetails == null) {
43-
errorHelper.sendUnauthorizedError(headerAccessor.getSessionId());
44-
return;
41+
throw new CustomException(ErrorCode.UNAUTHORIZED);
4542
}
4643

4744
Long fromUserId = userDetails.getUserId();
@@ -66,13 +63,10 @@ public void handleOffer(@Validated @Payload WebRTCOfferRequest request,
6663

6764
// WebRTC Answer 메시지 처리
6865
@MessageMapping("/webrtc/answer")
69-
public void handleAnswer(@Validated @Payload WebRTCAnswerRequest request,
70-
SimpMessageHeaderAccessor headerAccessor,
71-
Principal principal) {
66+
public void handleAnswer(@Validated @Payload WebRTCAnswerRequest request, Principal principal) {
7267
CustomUserDetails userDetails = WebSocketAuthHelper.extractUserDetails(principal);
7368
if (userDetails == null) {
74-
errorHelper.sendUnauthorizedError(headerAccessor.getSessionId());
75-
return;
69+
throw new CustomException(ErrorCode.UNAUTHORIZED);
7670
}
7771

7872
Long fromUserId = userDetails.getUserId();
@@ -97,13 +91,10 @@ public void handleAnswer(@Validated @Payload WebRTCAnswerRequest request,
9791

9892
// ICE Candidate 메시지 처리
9993
@MessageMapping("/webrtc/ice-candidate")
100-
public void handleIceCandidate(@Validated @Payload WebRTCIceCandidateRequest request,
101-
SimpMessageHeaderAccessor headerAccessor,
102-
Principal principal) {
94+
public void handleIceCandidate(@Validated @Payload WebRTCIceCandidateRequest request, Principal principal) {
10395
CustomUserDetails userDetails = WebSocketAuthHelper.extractUserDetails(principal);
10496
if (userDetails == null) {
105-
errorHelper.sendUnauthorizedError(headerAccessor.getSessionId());
106-
return;
97+
throw new CustomException(ErrorCode.UNAUTHORIZED);
10798
}
10899

109100
Long fromUserId = userDetails.getUserId();
@@ -127,13 +118,10 @@ public void handleIceCandidate(@Validated @Payload WebRTCIceCandidateRequest req
127118

128119
// 미디어 상태 토글 처리 (방 전체에 상태 공유)
129120
@MessageMapping("/webrtc/media/toggle")
130-
public void handleMediaToggle(@Validated @Payload WebRTCMediaToggleRequest request,
131-
SimpMessageHeaderAccessor headerAccessor,
132-
Principal principal) {
121+
public void handleMediaToggle(@Validated @Payload WebRTCMediaToggleRequest request, Principal principal) {
133122
CustomUserDetails userDetails = WebSocketAuthHelper.extractUserDetails(principal);
134123
if (userDetails == null) {
135-
errorHelper.sendUnauthorizedError(headerAccessor.getSessionId());
136-
return;
124+
throw new CustomException(ErrorCode.UNAUTHORIZED);
137125
}
138126

139127
Long userId = userDetails.getUserId();
@@ -150,15 +138,41 @@ public void handleMediaToggle(@Validated @Payload WebRTCMediaToggleRequest reque
150138

151139
// WebRTC 시그널링 처리 중 발생하는 CustomException 처리
152140
@MessageExceptionHandler(CustomException.class)
153-
public void handleCustomException(CustomException e, SimpMessageHeaderAccessor headerAccessor) {
154-
log.warn("WebRTC 시그널링 오류 발생: {}", e.getMessage());
155-
errorHelper.sendCustomExceptionToUser(headerAccessor.getSessionId(), e);
141+
public void handleCustomException(CustomException e, Principal principal) {
142+
if (principal == null) {
143+
log.warn("인증 정보 없는 사용자의 WebRTC 오류: {}", e.getMessage());
144+
return;
145+
}
146+
147+
log.warn("WebRTC 시그널링 오류 발생 (to {}): {}", principal.getName(), e.getMessage());
148+
149+
WebRTCErrorResponse errorResponse = WebRTCErrorResponse.from(e);
150+
151+
messagingTemplate.convertAndSendToUser(
152+
principal.getName(), // 에러를 발생시킨 사람의 username
153+
"/queue/webrtc",
154+
errorResponse
155+
);
156156
}
157157

158158
// 예상치 못한 모든 Exception 처리
159159
@MessageExceptionHandler(Exception.class)
160-
public void handleGeneralException(Exception e, SimpMessageHeaderAccessor headerAccessor) {
161-
log.error("WebRTC 시그널링 처리 중 예상치 못한 오류 발생", e);
162-
errorHelper.sendGenericErrorToUser(headerAccessor.getSessionId(), e, "시그널링 처리 중 오류가 발생했습니다.");
160+
public void handleGeneralException(Exception e, Principal principal) { // headerAccessor -> Principal
161+
if (principal == null) {
162+
log.error("WebRTC 처리 중 인증 정보 없는 사용자의 예외 발생", e);
163+
return;
164+
}
165+
166+
log.error("WebRTC 시그널링 처리 중 예상치 못한 오류 발생 (to {})", principal.getName(), e);
167+
168+
// CustomException으로 감싸서 일관된 형식의 에러 DTO를 생성
169+
CustomException customException = new CustomException(ErrorCode.WS_INTERNAL_ERROR);
170+
WebRTCErrorResponse errorResponse = WebRTCErrorResponse.from(customException);
171+
172+
messagingTemplate.convertAndSendToUser(
173+
principal.getName(),
174+
"/queue/webrtc",
175+
errorResponse
176+
);
163177
}
164178
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.back.global.websocket.webrtc.dto;
2+
3+
import com.back.global.exception.CustomException;
4+
import java.time.LocalDateTime;
5+
6+
public record WebRTCErrorResponse(
7+
String type,
8+
ErrorPayload error,
9+
LocalDateTime timestamp
10+
) {
11+
public record ErrorPayload(String code, String message) {}
12+
13+
public static WebRTCErrorResponse from(CustomException e) {
14+
return new WebRTCErrorResponse(
15+
"ERROR",
16+
new ErrorPayload(e.getErrorCode().getCode(), e.getMessage()),
17+
LocalDateTime.now()
18+
);
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.back.global.websocket.webrtc.dto.signal;
2+
3+
public record SdpData(String type, String sdp) {}

src/main/java/com/back/global/websocket/webrtc/dto/signal/WebRTCAnswerRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public record WebRTCAnswerRequest(
77
@NotNull Long roomId,
88
@NotNull Long targetUserId,
9-
@NotNull String sdp,
9+
@NotNull SdpData sdp,
1010
@NotNull WebRTCMediaType mediaType
1111
) {
1212
}

src/main/java/com/back/global/websocket/webrtc/dto/signal/WebRTCOfferRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public record WebRTCOfferRequest(
77
@NotNull Long roomId,
88
@NotNull Long targetUserId,
9-
@NotNull String sdp,
9+
@NotNull SdpData sdp,
1010
@NotNull WebRTCMediaType mediaType
1111
) {
1212
}

src/main/java/com/back/global/websocket/webrtc/dto/signal/WebRTCSignalResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public record WebRTCSignalResponse(
99
Long fromUserId,
1010
Long targetUserId,
1111
Long roomId,
12-
String sdp,
12+
SdpData sdp,
1313
WebRTCMediaType mediaType,
1414
String candidate,
1515
String sdpMid,
@@ -23,7 +23,7 @@ public static WebRTCSignalResponse offerOrAnswer(
2323
Long fromUserId,
2424
Long targetUserId,
2525
Long roomId,
26-
String sdp,
26+
SdpData sdp,
2727
WebRTCMediaType mediaType
2828
) {
2929
return new WebRTCSignalResponse(

0 commit comments

Comments
 (0)