From ff8aab08d54a92575c0ce08b8a18f3e14ac74f43 Mon Sep 17 00:00:00 2001 From: kimtaeyoung <62414583+kimtaeyoung201910794@users.noreply.github.com> Date: Mon, 19 Aug 2024 19:25:47 +0900 Subject: [PATCH] =?UTF-8?q?=ED=99=94=EC=83=81=EC=B1=84=ED=8C=85=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- majorLink/build.gradle | 3 ++ .../majorLink/global/config/WebConfig.java | 7 +++- .../global/config/WebSocketLiveConfig.java | 19 +++++++++++ .../handler/VideoChatWebSocketHandler.java | 33 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 majorLink/src/main/java/com/example/majorLink/global/config/WebSocketLiveConfig.java create mode 100644 majorLink/src/main/java/com/example/majorLink/handler/VideoChatWebSocketHandler.java diff --git a/majorLink/build.gradle b/majorLink/build.gradle index 8238b95..f8b21bb 100644 --- a/majorLink/build.gradle +++ b/majorLink/build.gradle @@ -57,6 +57,9 @@ dependencies { // iamport 결제 implementation 'com.github.iamport:iamport-rest-client-java:0.2.23' + + //web rtc 화상채팅 + implementation 'com.fasterxml.jackson.core:jackson-databind' } tasks.named('test') { diff --git a/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java b/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java index e9a28a7..4e4d4c7 100644 --- a/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java +++ b/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java @@ -10,7 +10,12 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") - .allowedOrigins("http://localhost:3000", "http://localhost:3007","http://localhost:3008") //프론트 테스트 url입니다. + .allowedOrigins("http://localhost:3000", + "http://localhost:3007","http://localhost:3008", + "http://localhost:59923", + "http://localhost:3003", + "http://localhost:3004" + ) //프론트 테스트 url입니다. .allowedMethods("POST", "GET", "DELETE", "PUT", "PATCH") .allowedHeaders("*"); } diff --git a/majorLink/src/main/java/com/example/majorLink/global/config/WebSocketLiveConfig.java b/majorLink/src/main/java/com/example/majorLink/global/config/WebSocketLiveConfig.java new file mode 100644 index 0000000..b7ccffd --- /dev/null +++ b/majorLink/src/main/java/com/example/majorLink/global/config/WebSocketLiveConfig.java @@ -0,0 +1,19 @@ +package com.example.majorLink.global.config; + +import com.example.majorLink.handler.VideoChatWebSocketHandler; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; + +@Configuration +@EnableWebSocket +public class WebSocketLiveConfig implements WebSocketConfigurer { + + //화상채팅 + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + registry.addHandler(new VideoChatWebSocketHandler(), "/ws/video-chat").setAllowedOrigins("*"); + + } +} diff --git a/majorLink/src/main/java/com/example/majorLink/handler/VideoChatWebSocketHandler.java b/majorLink/src/main/java/com/example/majorLink/handler/VideoChatWebSocketHandler.java new file mode 100644 index 0000000..dcde7a7 --- /dev/null +++ b/majorLink/src/main/java/com/example/majorLink/handler/VideoChatWebSocketHandler.java @@ -0,0 +1,33 @@ +package com.example.majorLink.handler; + +import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; +import org.springframework.web.socket.handler.TextWebSocketHandler; + +import java.util.HashSet; +import java.util.Set; + +public class VideoChatWebSocketHandler extends TextWebSocketHandler { + + private final Set sessions = new HashSet<>(); + + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + sessions.add(session); + } + + @Override + public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { + for (WebSocketSession webSocketSession : sessions) { + if(webSocketSession.isOpen() && !webSocketSession.equals(session)) { + webSocketSession.sendMessage(message); + } + } + } + + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { + sessions.remove(session); + } +}