【发布时间】:2019-07-29 17:02:35
【问题描述】:
这是关于使用 websocket + stompjs 发送推送通知的 spring boot + angularjs web 应用程序。
我最近从 Spring boot 1.2.0 升级到 2.1.3。 在之前,此升级 websocket(推送通知)工作了好几年。
我刚刚升级了 spring boot 和 websocket 相关的代码完全相同,但它现在不工作了。
不工作意味着:
- 在服务器端执行以下行没有任何错误/异常。
simpMessagingTemplate.convertAndSend("/topic/notify", payload);
- Chrome 调试器只接收“h”(心跳)而不是实际消息。
我不知道因为,
- 服务器端代码成功执行直到最后一行。
- websocket 会话建立,我可以收到心跳消息,但客户端也没有错误。
代码(但同样的代码适用于 Spring boot 1.2.0:
1.配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Value("${server.sessionTimeout}")
long sessionTimeoutInSecs;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/notify").withSockJS();
}
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
// in milliseconds
container.setMaxSessionIdleTimeout(sessionTimeoutInSecs * 1000);
return container;
}
}
2。消息发送代码:
simpMessagingTemplate.convertAndSend("/topic/notify", payload);
3.客户端代码:
(function() {
myApp.factory('autoUpdateTasksService', function($resource, $q, $log) {
var initSockets, notify, reconnect, socket, _callback;
_callback = null;
socket = {
client: null,
stomp: null
};
initSockets = function() {
socket.client = new SockJS('/notify');
socket.stomp = Stomp.over(socket.client);
socket.stomp.connect({}, function() {});
socket.client.onopen = function() {
var subscription1;
subscription1 = socket.stomp.subscribe("/topic/notify", notify);
//$log.log('socket connected');
};
};
reconnect = function() {
setTimeout(initSockets, 1000);
};
notify = function(message) {
try{
var taskNotifyObject;
if (message.body) {
taskNotifyObject = angular.fromJson(message.body);
//$log.log(taskNotifyObject);
var notificationArray=[];
notificationArray.push(taskNotifyObject);
_callback(notificationArray);
} else {
//$log.log("empty message");
}
} catch(e){
// alert(e.message);
}
};
return {
init: function(callback) {
_callback = callback;
initSockets();
}
};
});
}).call(this);
版本之间的spring框架有什么变化吗?
如何调试/查找消息丢失的位置?
【问题讨论】:
标签: spring spring-boot websocket spring-websocket stomp