Feat: WebSocket 환경설정
This commit is contained in:
parent
ba219fa208
commit
cb976ff556
@ -120,6 +120,9 @@ public class ProjectService {
|
||||
// 레디스 train 테이블에 존재하는지 확인
|
||||
|
||||
// AI서버와 웹 소켓 연결
|
||||
|
||||
// 웹 소켓 연결 후 AI에서 받은 메시지를 다른 곳에 전달
|
||||
|
||||
}
|
||||
|
||||
private Workspace getWorkspace(final Integer memberId, final Integer workspaceId) {
|
||||
|
@ -15,7 +15,7 @@ public class CorsMvcConfig implements WebMvcConfigurer {
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.exposedHeaders("Set-Cookie")
|
||||
.allowedOrigins(frontend, "http://localhost:5173") // application.yml에서 가져온 값 사용
|
||||
.allowedOrigins(frontend) // application.yml에서 가져온 값 사용
|
||||
.allowCredentials(true);
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,6 @@ public class RedisConfig {
|
||||
@Bean
|
||||
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory,
|
||||
MessageListenerAdapter listenerAdapter) {
|
||||
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.addMessageListener(listenerAdapter, new ChannelTopic("/ai/train"));
|
||||
|
@ -2,7 +2,6 @@ package com.worlabel.global.config;
|
||||
|
||||
import com.worlabel.global.handler.CustomWebSocketHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package com.worlabel.global.handler;
|
||||
|
||||
import com.worlabel.global.service.RedisMessageSubscriber;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.lang.NonNull;
|
||||
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.handler.TextWebSocketHandler;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CustomWebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
private final RedisTemplate<String , Object> redisTemplate;
|
||||
private final RedisMessageSubscriber redisMessageSubscriber;
|
||||
// private final RedisMessageSubscriber redisMessageSubscriber;
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(@NonNull WebSocketSession session) {
|
||||
redisMessageSubscriber.addSession(session);
|
||||
log.debug("연결 성공 - afterConnectionEstablished");
|
||||
// redisMessageSubscriber.addSession(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(@NonNull WebSocketSession session,@NonNull CloseStatus status) throws Exception {
|
||||
redisMessageSubscriber.removeSession(session);
|
||||
public void afterConnectionClosed(@NonNull WebSocketSession session,@NonNull CloseStatus status) {
|
||||
log.debug("연결 해제 - afterConnectionClosed");
|
||||
// redisMessageSubscriber.removeSession(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleTextMessage(@NonNull WebSocketSession session, TextMessage message) {
|
||||
log.debug("메세지 받음 - handleTextMessage : {}",message.getPayload());
|
||||
// Redis 메시지 발행
|
||||
redisTemplate.convertAndSend("/ai/train", message.getPayload());
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user