小编nii*_*zon的帖子

Spring Websocket:从SimpMessagingTemplate中不接收任何内容

我目前正在使用Spring-MVC webapp中的Websockets随时向spring-security-authenticated用户发送通知.重点是通知用户没有任何页面刷新或用户交互(例如Facebook).

因此,我正在使用Spring SimpMessagingTemplate

它看起来(如在浏览器控制台中所见)就像客户端正确订阅,并且SimpMessagingTemplate实际发送消息(调试Spring代码显示发生了快乐的流程sent == true等).但是,客户端没有任何事情发生 - 这就是问题所在.

经过几个小时的重新阅读参考,其他Stackoverflow帖子等(这与此处做同样的事情......但似乎工作!),我找不到合适的解决方案.唯一的区别是我有一个重复的websocket配置,这可能是我的麻烦的原因,但我无法摆脱(更多关于下面的内容:见dispatcher-servlet.xmlapplicationContext.xml).

客户端,我订阅如下:

<script>
    var socket = new SockJS('/stomp');
    var client = Stomp.over(socket);

    client.connect({}, function(s) {
            client.subscribe('/user/queue/notify', function (msg) {
            alert('SHOULD SEE THIS.. BUT DOES NOT WORK');
            console.log("SHOULD SEE THIS.. BUT DOES NOT WORK");
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

我有一个控制器,每10秒向用户"niilzon"发送一条简单的消息(这只是一个简单的测试)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

@Controller
public class WebsocketTestController {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Scheduled(fixedDelay = 10000)
    public void sendStuff() {
        messagingTemplate.convertAndSendToUser("niilzon", "/queue/notify", …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-security websocket

5
推荐指数
1
解决办法
2155
查看次数

标签 统计

spring ×1

spring-mvc ×1

spring-security ×1

websocket ×1