Skip to content

Commit

Permalink
Merge pull request #2 from urdego/Feature/#1
Browse files Browse the repository at this point in the history
Feature/#1
  • Loading branch information
Ban-gilhyeon authored Jan 27, 2025
2 parents a7620b3 + 19aafd3 commit 9c4e792
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ dependencyManagement {
dependencies {
//Spring
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

//WebSocket
implementation 'org.springframework.boot:spring-boot-starter-websocket'

// spring cloud
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.urdego.urdego_gateway_service.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayConfig {

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
// 알림 서비스로 라우팅
.route("notification-websocket-route", r -> r.path("/ws/notifications/**")
.uri("ws://notification-service:8081")) // 알림 서비스 URI

// 게임 서비스로 라우팅
.route("game-websocket-route", r -> r.path("/ws/games/**")
.uri("ws://game-service:8082")) // 게임 서비스 URI

// 기타 WebSocket 요청 처리 (필요하면 추가)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.urdego.urdego_gateway_service.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/urdego/sub");
registry.setApplicationDestinationPrefixes("/urdego/pub");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/urdego/connect")
.setAllowedOrigins("*");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.urdego.urdego_gateway_service.controller;

import io.urdego.urdego_gateway_service.dto.MessageInfo;
import io.urdego.urdego_gateway_service.feign.game.dto.GameServiceFeignClient;
import io.urdego.urdego_gateway_service.feign.notification.NotificationServiceFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
@RequiredArgsConstructor
public class WebSocketController {

private final NotificationServiceFeignClient notificationServiceFeignClient;
private final GameServiceFeignClient gameServiceFeignClient;
private final SimpMessagingTemplate messagingTemplate;

@MessageMapping("/message")
@SendTo("/urdego/sub")
public void handleMessage(MessageInfo message) {
switch (message.type()) {
case "알림 전송 요청":
// 알림 서비스로 메시지 전달 각각 UserId
notificationServiceFeignClient.sendNotification(message.payload());
break;

case "안읽은 알림 불러오기":
// 알림 서비스로 메시지 전달
notificationServiceFeignClient.sendNotification(message.payload());
break;

case "JOIN_ROOM":
// 방에 넣어버리는 메서드
gameServiceFeignClient.processGameEvent(message.payload());
break;
case "START_GAME":
// 게임 생성 및 시작 메서드

break;
// case "CHAT":
// // 메시지를 직접 브로드캐스트
// notificationServiceFeignClient.sendNotification(message.payload());
// messagingTemplate.convertAndSend("/urdego/sub/" + message.payload().userId(), message);
// break;
default:
throw new IllegalArgumentException("Unsupported message type: " + message.type());
}
}

@MessageMapping("/message")
@SendTo("/urdego/sub/{userId}")
public void handleMessage2(MessageInfo message, @DestinationVariable String userId) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.urdego.urdego_gateway_service.dto;

public record MessageInfo<T>(
String type,
T payload,
String userId
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.urdego.urdego_gateway_service.feign.game.dto;

public interface GameServiceFeignClient {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.urdego.urdego_gateway_service.feign.notification;

public interface NotificationServiceFeignClient {
//Notification의 모든 API

// 알림 전송

// 알림 확인 시 게임방으로 이동


}

0 comments on commit 9c4e792

Please sign in to comment.