diff options
author | Vitaly Takmazov | 2017-01-12 22:45:41 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-01-12 22:45:41 +0300 |
commit | fd8832275421bf2c6f17947aefa236bd65ef1650 (patch) | |
tree | b80a77e837f8c17f33b78ed603976fa3a2b11131 /juick-api/src/main/java/com/juick | |
parent | ffc944ae8cf95dd90b752444a6ce16b011bcd81e (diff) |
telegram: ping websocket
Diffstat (limited to 'juick-api/src/main/java/com/juick')
-rw-r--r-- | juick-api/src/main/java/com/juick/api/TGBot.java | 23 | ||||
-rw-r--r-- | juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java | 2 |
2 files changed, 25 insertions, 0 deletions
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 7e261540d..7456c8e78 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(); } @@ -64,6 +69,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 1781f5a61..5ca1def11 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 |