Spring Websocket : receiving nothing from SimpMessagingTemplate

标签 spring spring-mvc spring-security websocket

我目前在 Spring-MVC Web 应用程序中使用 Websockets 随时向经过 spring-security 身份验证的用户发送通知。重点是无需任何页面刷新或用户交互即可通知用户(例如 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>

我有一个 Controller ,每 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");
    }

}

浏览器控制台,“niilzon”用户登录后:

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

connected to server undefined
>>> SUBSCRIBE
id:sub-0
destination:/user/queue/notify

这是 XML 配置:

调度程序-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>

applicationContext.xml:

<websocket:message-broker>
    <websocket:stomp-endpoint path="/stomp">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic,/queue"/>
</websocket:message-broker>

dispatcher-servlet.xml 中所述,Websocket 配置在 applicationContext.xml 中重复。 如果我删除 dispatcher-servlet.xml 中的标记,客户端在尝试连接时会收到 404。 如果我删除 applicationContext.xml 中的标记,则无法在 WebsocketTestController 中 Autowiring SimpMessagingTemplate

配置重复可能是问题的一部分吗?这是我剩下的唯一线索,但我无法修复它。 需要注意的重要一点是,其他一些配置标记在 2 个 xml 文件之间是重复的。以下是两个配置文件之间的所有重复项,彼此相邻(+ 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>

网络应用程序的其余部分没有问题,它有很多 View 、 Controller 、服务、存储库等。

任何帮助将不胜感激!

最佳答案

感谢这里的 StackOverflow 帖子 Loading both mvc-dispatcher-servlet.xml and applicationContext.xml? ,我设法合并了 dispatcher-servletapplicationContext XML 中的重复项。

技巧就是简单地在 dispatcher-servlet 中导入 applicationContext,如下所示:

<import resource="applicationContext.xml" />

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

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

关于 Spring Websocket : receiving nothing from SimpMessagingTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31838385/

相关文章:

java - Spring StringTrimmerEditor 不工作

java - (Hibernate)Session.close()和releaseSession(Session)之间的区别

mysql - Grails bootstrap 有时无法保存

java - Spring MVC CSS 加载错误

java - 如何提供静态页面

spring-security-oauth2 2.0.7 刷新 token UserDetailsS​​ervice 配置 - UserDetailsS​​ervice 是必需的

spring - Spring MVC Java 中的 AuthenticationSuccessHandler 基于 Java 的配置

java - 使用 Tomcat 插件的 Jersey Web 应用程序未部署

java - 使用 ResponseEntity Spring MVC

java - 使用 springs @Value 注释的成员测试 Jersey 端点