From 21e3db95d7d2eddbbd4f901d8628b129804d13f6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 5 Apr 2017 11:46:55 +0300 Subject: juick-api: fix NPE and try to reconnect telegram bot --- juick-api/src/main/java/com/juick/api/TGBot.java | 13 +++++++++---- 1 file 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 -- cgit v1.2.3