diff options
-rw-r--r-- | juick-api/src/main/java/com/juick/api/TGBot.java | 13 |
1 files changed, 9 insertions, 4 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 7456c8e7..d665a197 100644 --- a/juick-api/src/main/java/com/juick/api/TGBot.java +++ b/juick-api/src/main/java/com/juick/api/TGBot.java @@ -20,6 +20,7 @@ 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.client.WebSocketConnectionManager; import org.springframework.web.socket.handler.TextWebSocketHandler; import javax.inject.Inject; @@ -42,6 +43,8 @@ public class TGBot extends TextWebSocketHandler { TelegramService telegramService; @Inject MessagesService messagesService; + @Inject + WebSocketConnectionManager connectionManager; WebSocketSession session; @@ -76,14 +79,16 @@ public class TGBot extends TextWebSocketHandler { @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { - logger.info("WebSocket disconnected with code {}: {}", status.getCode(), status.getReason()); - this.session = null; + logger.info("WebSocket disconnected with code {}: {}, trying to reconnect", status.getCode(), status.getReason()); + connectionManager.start(); } @Scheduled(fixedRate = 30000) public void ping() throws IOException { - logger.debug("Sending WebSocket ping"); - session.sendMessage(new PingMessage()); + if (session != null) { + logger.debug("Sending WebSocket ping"); + session.sendMessage(new PingMessage()); + } } @Override |