java Spring 4 STOMP Websockets 心跳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28841505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:15:48  来源:igfitidea点击:

Spring 4 STOMP Websockets Heartbeat

javaspringwebsocketstomp

提问by Dolan

I can't seem to find a good resource on how to send heartbeats to clients using websockets in Spring!

我似乎找不到关于如何在 Spring 中使用 websockets 向客户端发送心跳的好资源!

I have a basic server running using this configuration:

我有一个使用此配置运行的基本服务器:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/room");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/channels").withSockJS();
    }
}

Then I use something like this to send messages to people who subscribed to a room:

然后我使用这样的东西向订阅房间的人发送消息:

this.simpMessagingTemplate.convertAndSend("/room/" + this.roomId, message);

This is the client code used to communicate with the server:

这是用于与服务器通信的客户端代码:

this.connect = function (roomNameParam, connectionCallback) {
    var socket = new SockJS('http://localhost:8080/channels'),

    self.stompClient = Stomp.over(socket);
    self.stompClient.connect({}, function (frame) {
        self.stompClient.subscribe('/room/' + roomNameParam, connectionCallback);
    });
};

I really want to implement heartbeats so the client knows who is connected and to send some data to keep the client and server in sync.

我真的很想实现心跳,以便客户端知道谁连接并发送一些数据以保持客户端和服务器同步。

Do I need to manually do it?

我需要手动完成吗?

采纳答案by Bogdan

The Spring SockJS configuration contains settings for sending heartbeats. By default a heartbeat is sent every 25 seconds assuming no other messages are sent on the connection. See the Spring referencefor details.

Spring SockJS 配置包含发送心跳的设置。默认情况下,假设连接上没有发送其他消息,每 25 秒发送一次心跳。有关详细信息,请参阅Spring 参考

回答by Javatar

Just call:

只需致电:

.setTaskScheduler(heartBeatScheduler());

for the broker config where you want to enable it (works with simple broker too).

对于要启用它的代理配置(也适用于简单的代理)。

@Configuration
public class WebSocketMessageBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.setApplicationDestinationPrefixes("/app");
        config.enableSimpleBroker("/topic", "/queue", "/user")
                .setTaskScheduler(heartBeatScheduler());
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/endpoint");
    }

    @Bean
    public TaskScheduler heartBeatScheduler() {
        return new ThreadPoolTaskScheduler();
    }

}

回答by andrey_tr

For simple broker you can config heartbeat like this:

对于简单的代理,您可以像这样配置心跳:

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/wshandler" allowed-origins="*">
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic, /queue" heartbeat="10000,10000" scheduler="pingScheduler"/>
</websocket:message-broker>

<bean id="pingScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
    <property name="poolSize" value="1"/>
    <property name="threadNamePrefix" value="wss-heartbeat-thread-"/>
</bean>