forked from TeamMajorLink/majorLink_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π [HOTFIX] CORS μλ¬λ‘ μΈν΄ κΆν μμ 2
- Loading branch information
Showing
5 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
majorLink/src/main/java/com/example/majorLink/global/config/WebSocketLiveConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("*"); | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
majorLink/src/main/java/com/example/majorLink/handler/VideoChatWebSocketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |