我目前正在使用Spring-MVC webapp中的Websockets随时向spring-security-authenticated用户发送通知.重点是通知用户没有任何页面刷新或用户交互(例如Facebook).
因此,我正在使用Spring SimpMessagingTemplate
它看起来(如在浏览器控制台中所见)就像客户端正确订阅,并且SimpMessagingTemplate实际发送消息(调试Spring代码显示发生了快乐的流程sent == true等).但是,客户端没有任何事情发生 - 这就是问题所在.
经过几个小时的重新阅读参考,其他Stackoverflow帖子等(这与此处做同样的事情......但似乎工作!),我找不到合适的解决方案.唯一的区别是我有一个重复的websocket配置,这可能是我的麻烦的原因,但我无法摆脱(更多关于下面的内容:见dispatcher-servlet.xml和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>
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)