无法使用 Spring Websocket 发送用户消息 [英] Cannot send user message with Spring Websocket

查看:63
本文介绍了无法使用 Spring Websocket 发送用户消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关用户目的地的 Spring 文档.我想使用 convertAndSendToUser 方法只向特定用户发送消息.

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

这是java代码:

@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);
    }
}

这是客户端js代码:

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",...

stompClient.connect("myuser",...

为什么这个测试不起作用?该用户没有收到任何消息.如果我将目的地切换到 /topic/greetings 并将方法更改为 convertAndSend() 这可以工作,但显然是广播,而不仅仅是按要求发送给特定用户.

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.

我尝试使用此代码设置对单个用户的回复:

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

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

这有效,这不是我需要的,因为此回复仅回复来自客户端的消息.似乎我不能使用 convertAndSendToUser() 来处理来自服务器的未经请求的消息.

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.

推荐答案

也许回答这个问题为时已晚,但对于可能面临类似问题的人来说,这是我所缺少的:您连接的 websocket URL 也应该是一个安全的 URL.如果不是,则用户和会话之间没有映射,因此您无法使用 convertAndSendToUser() 向用户发送消息.

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().

所以,例如您正在使用 URL 模式 /hello 连接到端点,那么它应该是安全的.

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

这篇关于无法使用 Spring Websocket 发送用户消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆