Reputation: 2868
I am using Spring 4 + Websockets + Stomp JS library. I could not find any way to setup websocket ping/pong mechanism (heartbeat).
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...">
<websocket:message-broker>
<websocket:stomp-endpoint path="/cors/auth/clientEndpoint">
<websocket:handshake-handler ref="myHandshakeHandler" />
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/queue, /topic" />
<websocket:client-inbound-channel>
<websocket:interceptors>
<bean class="com.mycompany.myproject.utils.messaging.MyInboundChannelInterception"></bean>
</websocket:interceptors>
</websocket:client-inbound-channel>
</websocket:message-broker>
<bean id="myHandshakeHandler" class="com.mycompany.myproject.utils.security.MyHandshakeHandler" />
<bean class="org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean">
<property name="maxSessionIdleTimeout" value="120000" />
</bean>
As result, I am implementing my own mechanism of ping/pong messages.
One of the tasks here - to implement server side closure of the websocket in case if no ping message during more than 10s from client.
And no way to do this using Spring Websockets!
Maybe somebody can tell me how to access Session object of the user or to close those Session via Spring Websockets?
Seems Spring is very limited here.
Upvotes: 2
Views: 13877
Reputation: 945
I'm surprised spring doc doesn't mention how to config server ping...It seems that spring expects us to read code instead of read doc..
after some time searching on net and reading source code, I realize it's already supported, but not documented at a noticeable place like spring websocket doc.
I'm using spring 4.3.3, and here is how to config server ping without using sockJS:
@Configuration
@EnableWebSocketMessageBroker
public class StompOverWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").setAllowedOrigins("*");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
ThreadPoolTaskScheduler pingScheduler = new ThreadPoolTaskScheduler();
pingScheduler.initialize();
registry.enableSimpleBroker("/topic")
.setHeartbeatValue(new long[]{20000, 0}).setTaskScheduler(pingScheduler);
}
....
}
and should make sure that you correctly set web socket session timeout, it should be greater than ping interval, like this:
<bean id="servletServerContainerFactoryBean" class="org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean">
<property name="maxSessionIdleTimeout" value="30000"/>
</bean>
Upvotes: 5
Reputation: 382
To access websocket session you can use the following approach: https://stackoverflow.com/a/32270216/2982835
Upvotes: 0
Reputation: 59086
In this case, configuring SockJS in your app could go a long way:
<websocket:stomp-endpoint path="/cors/auth/clientEndpoint">
<websocket:handshake-handler ref="myHandshakeHandler" />
<websocket:sockjs/>
</websocket:stomp-endpoint>
This will give you:
If you want to actually close a session from STOMP endpoints, then I suggest you to vote/follow the SPR-12288 JIRA issue.
Upvotes: 4