|
| 1 | +package oncoding.concoder.controller; |
| 2 | + |
| 3 | +import java.text.ParseException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.UUID; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.json.simple.JSONObject; |
| 11 | +import org.json.simple.parser.JSONParser; |
| 12 | +import oncoding.concoder.dto.ChatDTO.SessionForTest; |
| 13 | +import org.springframework.boot.configurationprocessor.json.JSONException; |
| 14 | +import org.springframework.context.event.EventListener; |
| 15 | +import org.springframework.http.HttpEntity; |
| 16 | +import org.springframework.http.HttpHeaders; |
| 17 | +import org.springframework.messaging.handler.annotation.Header; |
| 18 | +import org.springframework.messaging.handler.annotation.MessageMapping; |
| 19 | +import org.springframework.messaging.handler.annotation.SendTo; |
| 20 | +import org.springframework.messaging.simp.SimpMessagingTemplate; |
| 21 | +import org.springframework.stereotype.Controller; |
| 22 | +import org.springframework.web.bind.annotation.RequestBody; |
| 23 | +import org.springframework.web.client.RestTemplate; |
| 24 | +import org.springframework.web.socket.messaging.SessionConnectEvent; |
| 25 | +import org.springframework.web.socket.messaging.SessionDisconnectEvent; |
| 26 | + |
| 27 | +@Slf4j |
| 28 | +@Controller |
| 29 | +@RequiredArgsConstructor |
| 30 | +public class VideoController { |
| 31 | + |
| 32 | + // 테스트용 세션 리스트. |
| 33 | + private final ArrayList<SessionForTest> sessionIdList; |
| 34 | + private final SimpMessagingTemplate template; |
| 35 | + |
| 36 | + // 실시간으로 들어온 세션 감지하여 전체 세션 리스트 반환 |
| 37 | + @MessageMapping("/video/joined-room-info") |
| 38 | + @SendTo("/sub/video/joined-room-info") |
| 39 | + private ArrayList<SessionForTest> joinRoom(@Header("simpSessionId") String sessionId, JSONObject ob) |
| 40 | + throws JSONException { |
| 41 | + |
| 42 | + // 현재 들어온 세션 저장. |
| 43 | + sessionIdList.add(new SessionForTest(UUID.fromString((String)ob.get("from")) , sessionId)); |
| 44 | + |
| 45 | + return sessionIdList; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + // caller의 정보를 다른 callee들에게 쏴준다. |
| 50 | + @MessageMapping("/video/caller-info") |
| 51 | + @SendTo("/sub/video/caller-info") |
| 52 | + private Map<String, Object> caller(JSONObject ob) throws JSONException { |
| 53 | + |
| 54 | + log.info(ob.toString()); |
| 55 | + |
| 56 | + // caller의 정보를 소켓으로 쏴준다. |
| 57 | + Map<String, Object> data = new HashMap<>(); |
| 58 | + data.put("toCall", ob.get("toCall")); |
| 59 | + data.put("from", ob.get("from")); |
| 60 | + data.put("signal", ob.get("signal")); |
| 61 | + |
| 62 | + return data; |
| 63 | + } |
| 64 | + |
| 65 | + // caller와 callee의 signaling을 위해 callee 정보를 쏴준다. |
| 66 | + @MessageMapping("/video/callee-info") |
| 67 | + @SendTo("/sub/video/callee-info") |
| 68 | + private Map<String, Object> answerCall(JSONObject ob) throws JSONException { |
| 69 | + |
| 70 | + log.info(ob.toString()); |
| 71 | + |
| 72 | + // accepter의 정보를 소켓으로 쏴준다. |
| 73 | + Map<String, Object> data = new HashMap<>(); |
| 74 | + data.put("signal", ob.get("signal")); |
| 75 | + data.put("from", ob.get("from")); |
| 76 | + data.put("to", ob.get("to")); |
| 77 | + |
| 78 | + return data; |
| 79 | + } |
| 80 | + |
| 81 | + @MessageMapping("/video/audio-sentiment") |
| 82 | + @SendTo("/sub/video/audio-sentiment") |
| 83 | + public Map<String, Object> getAudioSentiment(@RequestBody Map<String, String> map) |
| 84 | + throws ParseException, org.json.simple.parser.ParseException { |
| 85 | + |
| 86 | + HttpHeaders headers = new HttpHeaders(); |
| 87 | + RestTemplate restTemplate = new RestTemplate(); |
| 88 | + String resultMessage = restTemplate.postForObject( |
| 89 | + "http://localhost:8080" + "/audio-sentiment", new HttpEntity<>(map, headers), String.class); |
| 90 | + |
| 91 | + JSONParser parser = new JSONParser(); |
| 92 | + Object obj = parser.parse(resultMessage); |
| 93 | + JSONObject jsonObj = (JSONObject) obj; |
| 94 | + |
| 95 | + // {from : senderId, } |
| 96 | + Map<String, Object> returnData = new HashMap<>(); |
| 97 | + returnData.put("from", map.get("from")); |
| 98 | + returnData.put("resultOfAudioSentiment", jsonObj); |
| 99 | + return returnData; |
| 100 | + } |
| 101 | + |
| 102 | + @MessageMapping("/video/chat") |
| 103 | + @SendTo("/sub/video/chat") |
| 104 | + public Map<String, String> listenAndSendChat(@RequestBody Map<String, String> map) throws ParseException { |
| 105 | + |
| 106 | + return map; |
| 107 | + } |
| 108 | + |
| 109 | + @EventListener |
| 110 | + private void handleSessionConnected(SessionConnectEvent event) { |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + @EventListener |
| 115 | + private void handleSessionDisconnect(SessionDisconnectEvent event) { |
| 116 | + |
| 117 | + String removedID = ""; |
| 118 | + |
| 119 | + // close된 세션의 id 저장. |
| 120 | + for (SessionForTest session : sessionIdList) { |
| 121 | + if (session.getSessionId().equals(event.getSessionId())) { |
| 122 | + removedID = String.valueOf(session.getId()); |
| 123 | + sessionIdList.remove(session); |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + //종료 세션 id 전달. |
| 129 | + template.convertAndSend("/sub/video/close-session", removedID); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments