Feat: WebSocket 환경설정

This commit is contained in:
김용수 2024-09-11 18:00:06 +09:00
parent ba219fa208
commit cb976ff556
7 changed files with 48 additions and 7 deletions

View File

@ -120,6 +120,9 @@ public class ProjectService {
// 레디스 train 테이블에 존재하는지 확인 // 레디스 train 테이블에 존재하는지 확인
// AI서버와 소켓 연결 // AI서버와 소켓 연결
// 소켓 연결 AI에서 받은 메시지를 다른 곳에 전달
} }
private Workspace getWorkspace(final Integer memberId, final Integer workspaceId) { private Workspace getWorkspace(final Integer memberId, final Integer workspaceId) {

View File

@ -15,7 +15,7 @@ public class CorsMvcConfig implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") registry.addMapping("/**")
.exposedHeaders("Set-Cookie") .exposedHeaders("Set-Cookie")
.allowedOrigins(frontend, "http://localhost:5173") // application.yml에서 가져온 사용 .allowedOrigins(frontend) // application.yml에서 가져온 사용
.allowCredentials(true); .allowCredentials(true);
} }
} }

View File

@ -50,7 +50,6 @@ public class RedisConfig {
@Bean @Bean
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory, public RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) { MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer(); RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory); container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new ChannelTopic("/ai/train")); container.addMessageListener(listenerAdapter, new ChannelTopic("/ai/train"));

View File

@ -2,7 +2,6 @@ package com.worlabel.global.config;
import com.worlabel.global.handler.CustomWebSocketHandler; import com.worlabel.global.handler.CustomWebSocketHandler;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketConfigurer;

View File

@ -0,0 +1,19 @@
package com.worlabel.global.handler;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
public class AIWebSocketClient extends TextWebSocketHandler {
private WebSocketSession session;
public void connectToAIServer(){
StandardWebSocketClient client = new StandardWebSocketClient();
try{
session = client.doHandshake()
}
}
}

View File

@ -2,6 +2,7 @@ package com.worlabel.global.handler;
import com.worlabel.global.service.RedisMessageSubscriber; import com.worlabel.global.service.RedisMessageSubscriber;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -10,25 +11,29 @@ import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler; import org.springframework.web.socket.handler.TextWebSocketHandler;
@Slf4j
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class CustomWebSocketHandler extends TextWebSocketHandler { public class CustomWebSocketHandler extends TextWebSocketHandler {
private final RedisTemplate<String , Object> redisTemplate; private final RedisTemplate<String , Object> redisTemplate;
private final RedisMessageSubscriber redisMessageSubscriber; // private final RedisMessageSubscriber redisMessageSubscriber;
@Override @Override
public void afterConnectionEstablished(@NonNull WebSocketSession session) { public void afterConnectionEstablished(@NonNull WebSocketSession session) {
redisMessageSubscriber.addSession(session); log.debug("연결 성공 - afterConnectionEstablished");
// redisMessageSubscriber.addSession(session);
} }
@Override @Override
public void afterConnectionClosed(@NonNull WebSocketSession session,@NonNull CloseStatus status) throws Exception { public void afterConnectionClosed(@NonNull WebSocketSession session,@NonNull CloseStatus status) {
redisMessageSubscriber.removeSession(session); log.debug("연결 해제 - afterConnectionClosed");
// redisMessageSubscriber.removeSession(session);
} }
@Override @Override
protected void handleTextMessage(@NonNull WebSocketSession session, TextMessage message) { protected void handleTextMessage(@NonNull WebSocketSession session, TextMessage message) {
log.debug("메세지 받음 - handleTextMessage : {}",message.getPayload());
// Redis 메시지 발행 // Redis 메시지 발행
redisTemplate.convertAndSend("/ai/train", message.getPayload()); redisTemplate.convertAndSend("/ai/train", message.getPayload());
} }

View File

@ -0,0 +1,16 @@
package com.worlabel.global.service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class WebSocketMessageSubscriber {
public void onMessage(String message) {
// Redis 메세지를 수신하여 WebSocket 클라이언트에 전달 하기
log.debug("수신 - {} 이곳에서 클라이언트에게 전달",message);
}
}