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

nii*_*zon 5 spring spring-mvc spring-security websocket

我目前正在使用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", "SEEING THIS WOULD BE GREAT");
    }

}
Run Code Online (Sandbox Code Playgroud)

浏览器控制台,在"niilzon"用户登录后:

<<< CONNECTED
version:1.1
heart-beat:0,0
user-name:niilzon

connected to server undefined
>>> SUBSCRIBE
id:sub-0
destination:/user/queue/notify
Run Code Online (Sandbox Code Playgroud)

这是XML配置:

dispatcher-servlet.xml:

<!-- TODO this is duplicated in applicationContext !
 If removing the websocket tag in dispatcher-servlet : 404 when client tries to connect to /stomp
 If removing the websocket tag in applicationContext : cannot autowire SimpMessagingTemplate in WebsocketTestController
 -->
<websocket:message-broker>
    <websocket:stomp-endpoint path="/stomp">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic,/queue"/>
</websocket:message-broker>
Run Code Online (Sandbox Code Playgroud)

applicationContext.xml:

<websocket:message-broker>
    <websocket:stomp-endpoint path="/stomp">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic,/queue"/>
</websocket:message-broker>
Run Code Online (Sandbox Code Playgroud)

如上所述dispatcher-servlet.xml,websocket配置是重复的applicationContext.xml.如果我删除了标签dispatcher-servlet.xml,客户端在尝试连接时会获得404.如果我删除了标签applicationContext.xml,SimpMessagingTemplate则无法自动装入WebsocketTestController.

配置重复可能是问题的一部分吗?这是我离开的唯一领先优势,但我无法修复它.需要注意的一点是,在2个xml文件之间复制了一些其他配置标记.以下是2个配置文件之间的所有重复,彼此相邻(+如上所示的websocket配置):

<import resource="spring-security.xml"/>
<context:component-scan base-package="com.beatboxnow.**"/>
<tx:annotation-driven />
<!-- TODO merge / inherit contexts instead of this dupe ? How ? same with tx:annotationDriven -->
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
    <property name="shared" value="true"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

webapp的其余部分没有问题,它有很多视图,控制器,服务,存储库等.

任何帮助将不胜感激 !

nii*_*zon 3

感谢这里的 StackOverflow 帖子加载 mvc-dispatcher-servlet.xml 和 applicationContext.xml?,我设法合并了dispatcher-servletapplicationContextXML 中的重复项。

诀窍就是applicationContextdispatcher-servlet这样导入:

<import resource="applicationContext.xml" />
Run Code Online (Sandbox Code Playgroud)

,并删除所有重复项(applicationContext.xml显然不触及任何标签)。

自从这个项目开始以来,我就遇到了这个部分配置重复问题,并计划稍后将其删除 - 当时尝试导入,但当时我没能做到。现在这个 Websocket 问题迫使我再次尝试(复制之前没有阻塞),令人惊讶的是它工作得很好,我想我在第一次尝试期间犯了一个愚蠢的错误,因为它实际上是微不足道的:)