Skip to content

Commit

Permalink
Merge pull request #119 from chanu2/main
Browse files Browse the repository at this point in the history
📄: StompHandler SUBSCRIBE,CONNECT 수정
  • Loading branch information
GeonHui2 authored Oct 11, 2023
2 parents 1f9de0f + 7c6b913 commit 1618701
Showing 1 changed file with 68 additions and 127 deletions.
195 changes: 68 additions & 127 deletions src/main/java/ohsoontaxi/backend/global/config/StompHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ohsoontaxi.backend.domain.chat.presentation.dto.request.ChatMessageSaveDto;
import ohsoontaxi.backend.domain.chat.service.ChatRoomService;
import ohsoontaxi.backend.domain.participation.domain.Participation;
import ohsoontaxi.backend.domain.participation.service.ParticipationUtils;
import ohsoontaxi.backend.global.property.JwtProperties;
import ohsoontaxi.backend.global.redis.pub.RedisPublisher;
import ohsoontaxi.backend.global.security.JwtTokenProvider;
import ohsoontaxi.backend.global.utils.chat.ChatUtils;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
Expand All @@ -26,19 +22,13 @@
@RequiredArgsConstructor
@Slf4j
public class StompHandler implements ChannelInterceptor {
public static final String TOKEN = "token";
//public static final String TOKEN = "token";
public static final String SIMP_DESTINATION = "simpDestination";
public static final String SIMP_SESSION_ID = "simpSessionId";
public static final String INVALID_ROOM_ID = "InvalidRoomId";

private final ChatUtils chatUtils;

private final ChannelTopic topic;

private final ChatRoomService chatRoomService;

private final RedisPublisher redisPublisher;

private final ParticipationUtils participationUtils;

private final JwtTokenProvider jwtTokenProvider;
Expand All @@ -53,91 +43,42 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {

if (StompCommand.CONNECT == accessor.getCommand()) {

// String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());
//
// String token = jwtTokenProvider.resolveTokenWeb(rawHeader);
//
// log.info("token={}",token);
//
// jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));


// TODO: 2023/08/14 웹소켓 테스트

String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());

String token = jwtTokenProvider.resolveTokenWeb(rawHeader);

log.info("token={}",token);
jwtTokenProvider.resolveTokenWeb(rawHeader);

jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));
String userId = jwtTokenProvider.getUserId(token);

//String roomId = accessor.getFirstNativeHeader("roomId");
}

log.info("userId={}",userId);
// 소켓 연결 후 ,SUBSCRIBE 등록
else if (StompCommand.SUBSCRIBE == accessor.getCommand()) {

String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());

String destination = accessor.getFirstNativeHeader(SIMP_DESTINATION);
String token = jwtTokenProvider.resolveTokenWeb(rawHeader);

String sessionId = accessor.getFirstNativeHeader(SIMP_SESSION_ID);
jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));

// String destination = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_DESTINATION)
// ).orElse(INVALID_ROOM_ID);
String userId = jwtTokenProvider.getUserId(token);

// String sessionId = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_SESSION_ID)
// ).orElse(null);
String destination = Optional.ofNullable(
(String) message.getHeaders().get(SIMP_DESTINATION)
).orElse(INVALID_ROOM_ID);

log.info("sessionId={}",sessionId);
String sessionId = Optional.ofNullable(
(String) message.getHeaders().get(SIMP_SESSION_ID)
).orElse(null);

String roomId = chatUtils.getRoodIdFromDestination(destination);

Participation participation = participationUtils.findParticipation(Long.valueOf(userId), Long.valueOf(roomId));

chatRoomService.enterChatRoomTest(roomId, sessionId, participation.getUser().getEmail(), String.valueOf(participation.getId()));

// redisPublisher.publish(topic,
// ChatMessageSaveDto.builder()
// .type(ChatMessageSaveDto.MessageType.ENTER)
// .roomId(roomId)
// .userList(chatRoomService.findUser(roomId, sessionId))
// .build()
// );


}
// chatRoomService.enterChatRoomTest(roomId, sessionId, participation.getUser().getEmail(), String.valueOf(participation.getId()));

// 소켓 연결 후 ,SUBSCRIBE 등록
else if (StompCommand.SUBSCRIBE == accessor.getCommand()) {

// String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());
//
// String token = jwtTokenProvider.resolveTokenWeb(rawHeader);
//
// log.info("token={}",token);
//
// jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));
// String userId = jwtTokenProvider.getUserId(token);
//
// log.info("userId={}",userId);
//
//
// String destination = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_DESTINATION)
// ).orElse(INVALID_ROOM_ID);
//
// String sessionId = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_SESSION_ID)
// ).orElse(null);
//
// String roomId = chatUtils.getRoodIdFromDestination(destination);
//
// Participation participation = participationUtils.findParticipation(Long.valueOf(userId), Long.valueOf(roomId));
//
// chatRoomService.enterChatRoom(roomId, sessionId, participation.getUser().getName());
//

// redisPublisher.publish(topic,
// ChatMessageSaveDto.builder()
// .type(ChatMessageSaveDto.MessageType.ENTER)
Expand Down Expand Up @@ -209,32 +150,32 @@ else if (StompCommand.UNSUBSCRIBE == accessor.getCommand()) {
// TODO: 2023/08/21 세션ID 레디스에 다르게 저장


String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());

String token = jwtTokenProvider.resolveTokenWeb(rawHeader);

log.info("token={}",token);

jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));

String userId = jwtTokenProvider.getUserId(token);

log.info("userId={}",userId);


String destination = Optional.ofNullable(
(String) message.getHeaders().get(SIMP_DESTINATION)
).orElse(INVALID_ROOM_ID);

String sessionId = Optional.ofNullable(
(String) message.getHeaders().get(SIMP_SESSION_ID)
).orElse(null);

String roomId = chatUtils.getRoodIdFromDestination(destination);

Participation participation = participationUtils.findParticipation(Long.valueOf(userId), Long.valueOf(roomId));

chatRoomService.leaveChatRoomTest(String.valueOf(participation.getId()),roomId);
// String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());
//
// String token = jwtTokenProvider.resolveTokenWeb(rawHeader);
//
// log.info("token={}",token);
//
// jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));
//
// String userId = jwtTokenProvider.getUserId(token);
//
// log.info("userId={}",userId);
//
//
// String destination = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_DESTINATION)
// ).orElse(INVALID_ROOM_ID);
//
// String sessionId = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_SESSION_ID)
// ).orElse(null);
//
// String roomId = chatUtils.getRoodIdFromDestination(destination);
//
// Participation participation = participationUtils.findParticipation(Long.valueOf(userId), Long.valueOf(roomId));
//
// chatRoomService.leaveChatRoomTest(String.valueOf(participation.getId()),roomId);

// redisPublisher.publish(topic,
// ChatMessageSaveDto.builder()
Expand Down Expand Up @@ -270,31 +211,31 @@ else if (StompCommand.DISCONNECT == accessor.getCommand()) {
// );


String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());

String token = jwtTokenProvider.resolveTokenWeb(rawHeader);

log.info("token={}",token);

jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));
String userId = jwtTokenProvider.getUserId(token);

log.info("userId={}",userId);


String destination = Optional.ofNullable(
(String) message.getHeaders().get(SIMP_DESTINATION)
).orElse(INVALID_ROOM_ID);

String sessionId = Optional.ofNullable(
(String) message.getHeaders().get(SIMP_SESSION_ID)
).orElse(null);

String roomId = chatUtils.getRoodIdFromDestination(destination);

Participation participation = participationUtils.findParticipation(Long.valueOf(userId), Long.valueOf(roomId));

chatRoomService.disconnectWebsocketTest(String.valueOf(participation.getId()),roomId);
// String rawHeader = accessor.getFirstNativeHeader(jwtProperties.getHeader());
//
// String token = jwtTokenProvider.resolveTokenWeb(rawHeader);
//
// log.info("token={}",token);
//
// jwtTokenProvider.validateToken(jwtTokenProvider.resolveTokenWeb(rawHeader));
// String userId = jwtTokenProvider.getUserId(token);
//
// log.info("userId={}",userId);
//
//
// String destination = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_DESTINATION)
// ).orElse(INVALID_ROOM_ID);
//
// String sessionId = Optional.ofNullable(
// (String) message.getHeaders().get(SIMP_SESSION_ID)
// ).orElse(null);
//
// String roomId = chatUtils.getRoodIdFromDestination(destination);
//
// Participation participation = participationUtils.findParticipation(Long.valueOf(userId), Long.valueOf(roomId));
//
// chatRoomService.disconnectWebsocketTest(String.valueOf(participation.getId()),roomId);

// redisPublisher.publish(topic,
// ChatMessageSaveDto.builder()
Expand Down

0 comments on commit 1618701

Please sign in to comment.