Spring 4 STOMP Websockets Heartbeat

时间:2015-03-03 20:42:47

标签: java spring websocket stomp

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

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

@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();
    }
}

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

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

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

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);
    });
};

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

我需要手动操作吗?

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

请致电:

java.io.FileNotFoundException: class path resource [`FnmaUtils-3.2-fieldMapping.csv`] cannot be resolved to absolute file path because it does not reside in the file system: `jar:file:/home/ravibeli/.m2/repository/com/xxx/mismo/util/fnma-parser32/2018.1.0.0-SNAPSHOT/fnma-parser32-2018.1.0.0-SNAPSHOT.jar!/FnmaUtils-3.2-fieldMapping.csv`
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:218)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at com.xxx.fnma.util.FannieMaeUtils.<init>(FannieMaeUtils.java:41)
at com.xxx.fnma.processor.FNMA32Processor.<init>(FNMA32Processor.java:54)
at com.xxx.fnma.processor.FNMA32Processor.<clinit>(FNMA32Processor.java:43)

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

.setTaskScheduler(heartBeatScheduler());

答案 2 :(得分:2)

对于简单的经纪人,您可以像这样配置心跳:

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