本文介绍了Spring Websocket:从SimpMessagingTemplate接收不到任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Spring-MVC网络应用程序中使用Websockets随时向通过Spring安全性验证的用户发送通知.关键是要通知用户,而无需任何页面刷新或用户交互(例如Facebook).

I'm currently using Websockets in a Spring-MVC webapp to send notifications to spring-security-authenticated users at any time. The point is to notify users without any page refresh or user interaction (like Facebook does for example).

因此,我正在使用Spring的SimpMessagingTemplate

Therefore, I'm using Spring's SimpMessagingTemplate

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

It looks (as seen in the browser console) like the client subscribes correctly, and that the SimpMessagingTemplate actually sends a message (debugging the Spring code shows a happy flow happening, sent == true etc). However, nothing happens at client-side - which is the issue.

几个小时后,重新阅读了参考资料,其他Stackoverflow帖子等(与此处的功能相同.但是似乎可以正常工作!),我找不到合适的解决方案.唯一的区别是我有一个重复的websocket配置,这可能是造成我麻烦的原因,但我无法摆脱它(有关此内容的更多信息,请参见dispatcher-servlet.xmlapplicationContext.xml).

After hours of re-reading the reference, other Stackoverflow posts etc (which do the same thing as here.. But seem to work !), I can't find an appropriate solution. The only difference is that I have a duplicated websocket configuration, which might be the cause of my troubles, but that I cannot get rid of (more on that hereunder : see dispatcher-servlet.xml and applicationContext.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>

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

I have a controller that sends a simple message to user "niilzon" every 10 seconds (this is just a simple test ofcourse)

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"用户登录后:

Browser console, after the "niilzon" user logged in :

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

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

这是XML配置:

dispatcher-servlet.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>

applicationContext.xml:

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配置在中重复.如果删除dispatcher-servlet.xml中的标记,则客户端在尝试连接时会收到404.如果删除applicationContext.xml中的标签,则SimpMessagingTemplate无法在WebsocketTestController中自动接线.

As described in dispatcher-servlet.xml, the websocket configuration is duplicated in applicationContext.xml.If I remove the tag in dispatcher-servlet.xml, the client gets a 404 when trying to connect.If I remove the tag in applicationContext.xml, the SimpMessagingTemplate cannot be autowired in WebsocketTestController.

配置重复是否可能成为问题的一部分?那是我留下的唯一线索,但我无法解决.需要注意的重要一点是,其他两个配置标记在2个xml文件之间重复.这是两个配置文件之间所有重复的文件,彼此相邻(+如上所示的websocket配置):

Could the configuration duplication be part of the issue ? That's the only lead I have left, but I am unable to fix it.An important thing to note is that some other config tags are duplicated between the 2 xml files. Here are ALL the duplications between the 2 config files, put next to each other (+ the websocket config as seen above) :

<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>

该Web应用程序的其余部分没有问题,该Web应用程序具有很多视图,控制器,服务,存储库等.

There are no issues with the rest of the webapp, which has alot of views, controllers, services, repositories etc.

任何帮助将不胜感激!

推荐答案

感谢此处的StackOverflow帖子,我设法将dispatcher-servletapplicationContext XML中的重复项合并.

Thanks to this StackOverflow post here Loading both mvc-dispatcher-servlet.xml and applicationContext.xml?, I managed to merge the duplicates in the dispatcher-servlet and applicationContext XML's.

诀窍就是像这样将applicationContext导入dispatcher-servlet中:

The trick was simply to import the applicationContext in the dispatcher-servlet like this :

<import resource="applicationContext.xml" />

,并删除所有重复项(显然不要触摸applicationContext.xml中的任何标签).

, and to remove all the duplicates (not touching any tag in applicationContext.xml obviously).

自该项目开始以来,我就一直存在这个部分配置重复的问题,并计划在以后将其删除-当时尝试了导入,但是那时我没有做到这一点.现在这个Websocket问题迫使我再试一次(复制以前没有阻塞),而且令人惊讶的是它运行得很好,我想我在第一次尝试时犯了一个愚蠢的错误,因为它实际上是很琐碎的:)

I had this partial configuration duplication issue hanging since the beginning of this project and planned to remove it later on - tried the import at that time but I did not manage to do it back then.Now this Websocket issue forced me to try it again (duplicating was not blocking before), and surprisingly it worked perfectly, I suppose that I made a silly mistake during the first try, since it was in fact rather trivial :)

这篇关于Spring Websocket:从SimpMessagingTemplate接收不到任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:01