Cannot send user message with Spring Websocket

匆匆过客 提交于 2020-01-04 05:28:07

问题


I read Spring docs about user destinations. I would like to use convertAndSendToUser method to send a message only to a particular user.

This is the java code:

@Controller
public class WebsocketTest {

    @Autowired
    public SimpMessageSendingOperations messagingTemplate;

    @PostConstruct
    public void init(){
        ScheduledExecutorService statusTimerExecutor=Executors.newSingleThreadScheduledExecutor();
        statusTimerExecutor.scheduleAtFixedRate(new Runnable() {                
            @Override
            public void run() {
                messagingTemplate.convertAndSendToUser("myuser","/queue/test", new Return("test"));
            }
        }, 5000,5000, TimeUnit.MILLISECONDS);
    }
}

This is the client js code:

var socket = new WebSocket('ws://localhost:8080/hello');
            stompClient = Stomp.over(socket);            
            stompClient.connect("myuser","mypass", function(frame) {
                setConnected(true);
                console.log('Connected: ' + frame);
                stompClient.subscribe('/topic/greetings', function(greeting){
                    showGreeting(JSON.parse(greeting.body).value);
                });
                stompClient.subscribe('/user/queue/test', function(greeting){
                    showGreeting(JSON.parse(greeting.body).value);
                });
            });

First of all is it correct to conside the user value this one?

stompClient.connect("myuser",...

Why this test doesn't work? This user did not receveive any message. If I switch destination to /topic/greetings and change method to convertAndSend() this works, but obviously as broadcast and not only to particular user as requested.

Little update

I tried to setup a reply to single user with this code:

    @MessageMapping("/hello")
    @SendToUser("/queue/test")
    public Return test(){
        return new Return("test");
    }

This works, this is not what I need becase this reply only to a message from client. Seems that I cannot use convertAndSendToUser() for unsollicited messaged from server.


回答1:


Subscribing to /user/queue/test on the client side and using convertAndSendToUser to send a message to the /topic/test topic looks about right.

Now how are you getting the "myUser" value?

You can get this value from either request.getUserPrincipal() during the handshake, or inject the Principal when receiving a message; the sessionId works as well here. You can check this commit for more details on the matter.




回答2:


Maybe its late to answer the question but for the people who may face similar issue, here is what I was missing: The websocket URL which you are connecting to should also be a secured URL. If its not then there is no mapping between the user and the session and hence you are not able to send message to the user using convertAndSendToUser().

So, for e.g. you are connecting to the endpoint with URL pattern /hello then it should be a secured one.



来源:https://stackoverflow.com/questions/34942934/cannot-send-user-message-with-spring-websocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!