【发布时间】:2015-07-21 00:36:41
【问题描述】:
我在前端使用STOMP.js,在后端使用ActiveMQ 向客户端发送推送通知。客户端先订阅一个主题,代码如下:
function stompConnect() {
console.log('STOMP: Attempting connection');
// recreate the stompClient to use a new WebSocket
var socket = new SockJS('/websocket');
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
stompClient.subscribe('/topic/table-updates', function(notification){
showNotification(JSON.parse(notification.body));
});
}, function (error) {
console.log('STOMP: ' + error);
setTimeout(stompConnect, 10000);
console.log('STOMP: Reconnecting in 10 seconds');
});
}
stompConnect();
有时底层 websocket 连接丢失,客户端需要重新连接并再次订阅主题(超时 10 秒)。这会导致在客户端重新连接时,来自服务器的一些消息会丢失。有什么办法可以预防吗?
我在后端使用 Spring WebSocket。这是配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Value("${stomp.port}")
private Integer stompPort;
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry
.enableStompBrokerRelay("/topic/")
.setRelayPort(stompPort);
}
@Bean(initMethod = "start", destroyMethod = "stop")
public BrokerService brokerService() throws Exception {
final BrokerService broker = BrokerFactory.createBroker(
String.format("broker:(vm://localhost,stomp://localhost:%d)?persistent=false", stompPort));
broker.addShutdownHook(new SpringContextHook());
return broker;
}
}
【问题讨论】:
-
检查是否可以配置 STOMP 客户端库以创建在 ActiveMQ STOMP page 上定义的持久订阅者
标签: websocket activemq stomp spring-websocket