aboutsummaryrefslogtreecommitdiff
path: root/juick-ws/src/main/java/com/juick
diff options
context:
space:
mode:
Diffstat (limited to 'juick-ws/src/main/java/com/juick')
-rw-r--r--juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java13
-rw-r--r--juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java2
2 files changed, 15 insertions, 0 deletions
diff --git a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
index 32888797..81f636a7 100644
--- a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
+++ b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
@@ -10,7 +10,9 @@ import org.apache.http.client.utils.URLEncodedUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
+import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.CloseStatus;
+import org.springframework.web.socket.PingMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@@ -117,6 +119,17 @@ public class WebsocketComponent extends TextWebSocketHandler {
}
+ @Scheduled(fixedRate = 30000)
+ public void ping() {
+ clients.forEach(c -> {
+ try {
+ c.session.sendMessage(new PingMessage());
+ } catch (IOException e) {
+ logger.error("WebSocket PING exception", e);
+ }
+ });
+ }
+
public List<SocketSubscribed> getClients() {
return clients;
}
diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
index fea6959f..e0a31b13 100644
--- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
+++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
@@ -6,6 +6,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
+import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistry;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
@@ -18,6 +19,7 @@ import javax.inject.Inject;
* Created by aalexeev on 11/24/16.
*/
@Configuration
+@EnableScheduling
@EnableWebSocket
@PropertySource("classpath:juick.conf")
class WebsocketConfiguration implements WebSocketConfigurer {