-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
705a2ca
commit 692bff8
Showing
6 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
server/src/main/java/com/example/spring_websocket/controller/WebSocketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.spring_websocket.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; | ||
import org.springframework.messaging.simp.user.SimpUserRegistry; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.messaging.SessionConnectEvent; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class WebSocketHandler { | ||
/** | ||
* STOMP 서브 프로토콜을 통한 CONNECT 메시지가 수신되면 발생하는 이벤트 {@link SessionConnectEvent}를 처리합니다. | ||
* <p> STOMP CONNECT 메시지 헤더에서 nickname을 추출해 웹소켓 세션 헤더에 저장합니다. | ||
*/ | ||
@EventListener | ||
public void handleSessionConnect(SessionConnectEvent event) { | ||
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(event.getMessage()); | ||
String nickname = accessor.getFirstNativeHeader("nickname"); | ||
|
||
// 세션에 닉네임 저장 | ||
accessor.getSessionAttributes().put("nickname", nickname); | ||
|
||
// 로깅을 위해 sessionId와 함께 보여줍니다 | ||
String sessionId = accessor.getSessionId(); | ||
log.info("sessionId: " + sessionId + ", nickname: " + nickname); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
public class MessageResponseDto { | ||
private String content; | ||
private String sessionId; | ||
private String nickname; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters