Skip to content

Commit

Permalink
πŸš‘ [HOTFIX] CORS μ—λŸ¬λ‘œ 인해 κΆŒν•œ μˆ˜μ •2
Browse files Browse the repository at this point in the history
  • Loading branch information
kchaeeun committed Aug 19, 2024
2 parents 23c929f + 5d05557 commit 79b07c3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
1 change: 0 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions majorLink/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ 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", "OPTIONS")
.allowedHeaders("Content-Type", "Authorization")
.allowedHeaders("*")
.allowCredentials(true);
}
}
Original file line number Diff line number Diff line change
@@ -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("*");

}
}
Original file line number Diff line number Diff line change
@@ -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<WebSocketSession> 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);
}
}

0 comments on commit 79b07c3

Please sign in to comment.