From fd8832275421bf2c6f17947aefa236bd65ef1650 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 12 Jan 2017 22:45:41 +0300 Subject: telegram: ping websocket --- juick-api/src/main/java/com/juick/api/TGBot.java | 23 ++++++++++++++++++++++ .../api/configuration/ApiAppConfiguration.java | 2 ++ 2 files changed, 25 insertions(+) (limited to 'juick-api/src/main/java/com') diff --git a/juick-api/src/main/java/com/juick/api/TGBot.java b/juick-api/src/main/java/com/juick/api/TGBot.java index 7e261540..7456c8e7 100644 --- a/juick-api/src/main/java/com/juick/api/TGBot.java +++ b/juick-api/src/main/java/com/juick/api/TGBot.java @@ -15,6 +15,9 @@ import com.pengrad.telegrambot.response.SendResponse; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.PingMessage; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; @@ -40,6 +43,8 @@ public class TGBot extends TextWebSocketHandler { @Inject MessagesService messagesService; + WebSocketSession session; + private TGBot() { throw new IllegalStateException(); } @@ -63,6 +68,24 @@ public class TGBot extends TextWebSocketHandler { } } + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + logger.info("WebSocket connected"); + this.session = session; + } + + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { + logger.info("WebSocket disconnected with code {}: {}", status.getCode(), status.getReason()); + this.session = null; + } + + @Scheduled(fixedRate = 30000) + public void ping() throws IOException { + logger.debug("Sending WebSocket ping"); + session.sendMessage(new PingMessage()); + } + @Override protected void handleTextMessage(WebSocketSession session, TextMessage text) throws Exception { com.juick.Message jmsg = ms.readValue(text.asBytes(), com.juick.Message.class); diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java index 1781f5a6..5ca1def1 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java @@ -7,6 +7,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.client.WebSocketConnectionManager; import org.springframework.web.socket.client.standard.StandardWebSocketClient; @@ -16,6 +17,7 @@ import javax.inject.Inject; * Created by aalexeev on 11/12/16. */ @Configuration +@EnableScheduling @PropertySource("classpath:juick.conf") public class ApiAppConfiguration { @Inject -- cgit v1.2.3