Skip to content

Commit 41a38ba

Browse files
authored
Merge pull request #35 from prgrms-web-devcourse-final-project/Feat/33
Feat: WebSocket + STOMP 기본 설정 구현 (#33)
2 parents eae41f4 + f0aafac commit 41a38ba

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ repositories {
2727
dependencies {
2828
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
2929
implementation("org.springframework.boot:spring-boot-starter-web")
30+
implementation("org.springframework.boot:spring-boot-starter-websocket")
3031
implementation("org.springframework.boot:spring-boot-starter-security")
3132
testImplementation("org.springframework.security:spring-security-test")
3233
compileOnly("org.projectlombok:lombok")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.back.domain.websocket.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
5+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
6+
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
7+
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
8+
9+
@Configuration
10+
@EnableWebSocketMessageBroker
11+
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
12+
13+
/**
14+
* 메시지 브로커 설정
15+
* - /topic: 1:N 브로드캐스트 (방 채팅)
16+
* - /queue: 1:1 메시지 (개인 DM)
17+
* - /app: 클라이언트에서 서버로 메시지 전송 시 prefix
18+
*/
19+
@Override
20+
public void configureMessageBroker(MessageBrokerRegistry config) {
21+
config.enableSimpleBroker("/topic", "/queue");
22+
config.setApplicationDestinationPrefixes("/app");
23+
config.setUserDestinationPrefix("/user");
24+
}
25+
26+
/**
27+
* STOMP 엔드포인트 등록
28+
* 클라이언트가 WebSocket 연결을 위해 사용할 엔드포인트
29+
*/
30+
@Override
31+
public void registerStompEndpoints(StompEndpointRegistry registry) {
32+
registry.addEndpoint("/ws")
33+
.setAllowedOriginPatterns("*") // 모든 도메인 허용 (개발용)
34+
.withSockJS(); // SockJS 사용
35+
}
36+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.back.domain.websocket.controller;
2+
3+
import com.back.global.common.dto.RsData;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import java.time.LocalDateTime;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
@Slf4j
16+
@RestController
17+
@RequestMapping("/api/websocket")
18+
public class WebSocketTestController { // WebSocket 기능 테스트용 REST 컨트롤러
19+
20+
// WebSocket 서버 상태 확인
21+
@GetMapping("/health")
22+
public ResponseEntity<RsData<Map<String, Object>>> healthCheck() {
23+
log.info("WebSocket 헬스체크 요청");
24+
25+
Map<String, Object> data = new HashMap<>();
26+
data.put("service", "WebSocket");
27+
data.put("status", "running");
28+
data.put("timestamp", LocalDateTime.now());
29+
data.put("endpoints", Map.of(
30+
"websocket", "/ws",
31+
"chat", "/app/rooms/{roomId}/chat",
32+
"join", "/app/rooms/{roomId}/join",
33+
"leave", "/app/rooms/{roomId}/leave"
34+
));
35+
36+
return ResponseEntity
37+
.status(HttpStatus.OK)
38+
.body(RsData.success("WebSocket 서비스가 정상 동작중입니다.", data));
39+
}
40+
41+
// WebSocket 연결 정보 제공
42+
@GetMapping("/info")
43+
public ResponseEntity<RsData<Map<String, Object>>> getConnectionInfo() {
44+
log.info("WebSocket 연결 정보 요청");
45+
46+
Map<String, Object> connectionInfo = new HashMap<>();
47+
connectionInfo.put("websocketUrl", "/ws");
48+
connectionInfo.put("sockjsSupport", true);
49+
connectionInfo.put("stompVersion", "1.2");
50+
connectionInfo.put("subscribeTopics", Map.of(
51+
"roomChat", "/topic/rooms/{roomId}/chat",
52+
"privateMessage", "/user/queue/messages",
53+
"notifications", "/user/queue/notifications"
54+
));
55+
connectionInfo.put("sendDestinations", Map.of(
56+
"roomChat", "/app/rooms/{roomId}/chat",
57+
"joinRoom", "/app/rooms/{roomId}/join",
58+
"leaveRoom", "/app/rooms/{roomId}/leave"
59+
));
60+
61+
return ResponseEntity
62+
.status(HttpStatus.OK)
63+
.body(RsData.success("WebSocket 연결 정보", connectionInfo));
64+
}
65+
}

src/main/java/com/back/global/entity/BaseEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public abstract class BaseEntity {
1616
private Long id;
1717

1818
@CreatedDate
19-
private LocalDateTime createAt;
19+
private LocalDateTime createdAt;
2020

2121
@LastModifiedDate
2222
private LocalDateTime updatedAt;

0 commit comments

Comments
 (0)