일전에 Stomp를 통해 채팅 시스템을 구축한 적이 있다.

이 경우 subscribe된 유저들에게 전송을 할 경우 convertAndSend 메소드를 사용하면 손쉽게 브로드캐스트를 할 수 있었다.

 

그러나 이번 프로젝트의 경우, 웹소켓 요청을 보낸 클라이언트에게 그대로 메시지 return을 해줘야하는 경우가 생겼다.

 

이 경우, convertAndSendTouser 메소드를 이용해 특정 유저에게 메시지를 전송 할 수 있다.

@Override
    public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException {
        convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null);
    }

 

이게 메소드의 기본 구조인데, user 자리에 실제 유저의 sessionid, subscribe된 주소, 그리고 메시지를 담아 보내면 가볍게 전송이 완료될 줄 알았는데, 실제로 그렇지 않았다.

 

유저의 고유값이라고 할 수 있는 sessionId와 정확한 destination 또한 입력하였지만 작동을 하지 않아 구글링을 해보았다.

 

 

https://stackoverflow.com/questions/59456275/header-problems-with-use-of-convertandsendtouser-method-in-spring-websocket

 

Header problems with use of convertAndSendToUser method in Spring WebSocket

I'm trying to send a message directly to an user using SimpMessageSendingOperations or SimpMessagingTemplate but both doesn't work. simpMessagingTemplate.convertAndSend("/user/" + userSessionId + "/

stackoverflow.com

 

 

같은 주제로 올라온 게시물이 있었다(구글링 최고)

내용을 요약해 보자면다음과 같다.

1. convertAndSendToUser를 사용하기 위해 불러 오는 객체 SimpleMessagingTemplateAbstractMessageSendingTemplate을 상속받는다.

 

 

2. AbstractMessageSendingTemplate 은 메시지를 전송하기 위한 header 세팅을 별도로 진행 해준다.

 

 

3. SimpleMessagingTemplate 은 Native header가 있을 경우, 같은 header로 return 해주는데 이 때 내가 가지고 있던 헤더가 HashMap 바뀌게 되어 동작을 하지 않는 것이다.

우리가 원래의 header 정보를 사용하고 싶다면, Messageheader 타입의 헤더정보가 필요하다.

 

 

4. 그렇기 때문에, 메시지 전송을 위해 헤더 세팅을 별도로 해주어야 정상적으로 메시지를 보낼 수 있다.

 

헤더 세팅
전송

 

+ Recent posts