Skip to content

Commit

Permalink
Feat : STOMP 개인 호출 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
Mouon committed Aug 29, 2024
1 parent 31f4ea9 commit 5f95c74
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 175 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
package com.linkode.api_server.controller;

import com.linkode.api_server.service.MemberStudyroomService;
import com.linkode.api_server.service.WebSocketSessionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.*;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.messaging.WebSocketStompClient;

@Controller
@Slf4j
@RequiredArgsConstructor
public class SignalingController {

private final SimpMessagingTemplate messagingTemplate;
private final WebSocketSessionService sessionService;
private final MemberStudyroomService memberStudyroomService;

/**
* 전체 메세지 (SUBSCRIBE)
*/
@MessageMapping("/studyroom/{studyroomId}/sendMessage")
@SendTo("/topic/studyroom/{studyroomId}")
public String handleStudyroomMessage(@DestinationVariable String studyroomId,
Expand All @@ -38,43 +26,32 @@ public String handleStudyroomMessage(@DestinationVariable String studyroomId,
if (type == null) {
return "Invalid message type";
}

switch (type) {
case "join":
return "User " + payload.get("userId") + " has joined the room.";

case "leave":
return "User " + payload.get("userId") + " has left the room.";

case "message":
return "User " + memberId + ": " + payload.get("content");

default:
return "Unknown message type";
}
}

/** 이상합니다.. 세션을 어떤거 기반으로 추출해야할지 혼동... */

/**
* 개인 호출
*/
@MessageMapping("/studyroom/{studyroomId}/callUser")
public void callUser(@DestinationVariable String studyroomId,
SimpMessageHeaderAccessor headerAccessor,
@Payload String targetUserId,
Principal principal) { // Principal 객체 추가

// Principal에서 부르는 사람 추출
Long callerId = (Long) headerAccessor.getSessionAttributes().get("memberId");
log.info("Attempting to call user: " + targetUserId + " from user: " + callerId);

// 특정 사용자의 세션을 가져와 메시지 전송
WebSocketSession targetSession = sessionService.getSession(targetUserId);
if (targetSession != null && targetSession.isOpen()) {
messagingTemplate.convertAndSendToUser(targetUserId, "/queue/private",
"Call from user " + callerId + " in studyroom " + studyroomId);
@MessageMapping("/studyroom/{studyroomId}/call/{targetUserId}")
@SendTo("/topic/studyroom/{studyroomId}/targetUser/{targetUserId}")
public Map<String, Object> handleCallMessage(@DestinationVariable String studyroomId,
@DestinationVariable String targetUserId,
@Payload Map<String, Object> payload) {
String callerId = (String) payload.get("callerId");
if (callerId == null) {
log.error("Caller ID is null");
return null;
}
Map<String, Object> messageContent = new HashMap<>();
messageContent.put("type", "call");
messageContent.put("callerId", callerId);
messageContent.put("studyroomId", studyroomId);
return messageContent;
}

}
}
Loading

0 comments on commit 5f95c74

Please sign in to comment.